Working with Dates in Python
To work with dates in Python, you need to import the corresponding modules. This is done using a special instruction import, followed by the name of the module. The import is written in the top line of the file.
Let's import the datetime module for basic date handling:
import datetime
To display the date, a special method date should be applied to the imported module. Its parameters specify year, month, day. For convenience, we will write the received date to the variable res:
res = datetime.date(2025, 12, 31)
print(res) # 2025-12-31
In essence, the variable res now stores an object containing the date. It is useful because it can be used to derive more detailed characteristics of the date: day, month, year, and so on. To do this, you just need to access its properties - day, month, year:
print(res.day) # 31 print(res.month) # 12 print(res.year) # will be released in 2025
Set the variable birthdate to the user's birth date. Then print it in the format year-month-day.
Modify the previous task to only print the user's year of birth.
Get the day the user was born from birthdate.
Get the user's birth month from birthdate.