Screen Link:
My Code:
Hi everyone,
There are few aspects from the chapter “Working with Dates and Times in Python” that are not entirely clear to me, so I was wondering if someone could help me out to clarify them:
-In order to call the strptime constructor, we need to write the following code:
date_1_str = “24/12/1984”
date_1_dt = dt.datetime.strptime(date_1_str, “%d/%m/%Y”)
print(type(date_1_dt))
print(date_1_dt)
That means for date_1_dt we need to write: module(dt).datetime(class).strptime(constructor)
My question is the following: does this also apply for the strftime constructor? Would the coding be like this?:
module(dt).datetime(class).strftime(constructor)
-When using the strptime constructor to parse string as dates (mission 5), the coding is as follows:
for row in potus:
appt_start_date_str = str(row[2])
appt_start_date = dt.datetime.strptime(appt_start_date_str, date_format)
row[2] = appt_start_date
In this case, after changing the variable apt_start_date_str from a string to a datetime format, we need to reassign this value to row[2] (last line of code).
However, when using the strftime constructor to format datetime objects to strings (mission 6), the coding is as follows:
for row in potus:
app_start_date = row[2]
app_start_date_str = app_start_date.strftime("%B, %Y")
if app_start_date_str not in visitors_per_month:
visitors_per_month[app_start_date_str] = 1
else:
visitors_per_month[app_start_date_str] += 1
In this case, we do not need to reassign the value of app_start_date_str back to row[2] (there is no line with row[2] = app_start_date_str in the code). Could please someone explain to me why reassigning the value back to row[2] is needed and why only for one of the cases?
Many thanks in advance
Replace this line with your code
What I expected to happen:
What actually happened:
Replace this line with the output/error
<!--Enter other details below: -->