0
0
Pythonprogramming~10 mins

Date and time handling in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the module needed for date and time handling.

Python
import [1]
Drag options to blanks, or click blank then click option'
Adatetime
Btimeit
Crandom
Dos
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated modules like 'random' or 'os'.
Confusing 'timeit' with 'datetime'.
2fill in blank
medium

Complete the code to get the current date and time.

Python
now = datetime.[1]()
Drag options to blanks, or click blank then click option'
Atime
Bdate
Cdatetime
Dtimedelta
Attempts:
3 left
💡 Hint
Common Mistakes
Using date() which returns only the date.
Using time() which is not a class method here.
3fill in blank
hard

Fix the error in the code to format the current date as 'YYYY-MM-DD'.

Python
formatted_date = now.[1]("%Y-%m-%d")
Drag options to blanks, or click blank then click option'
Aformat
Bstrptime
Cto_string
Dstrftime
Attempts:
3 left
💡 Hint
Common Mistakes
Using strptime() which parses strings to datetime.
Using non-existent methods like to_string().
4fill in blank
hard

Fill both blanks to create a timedelta of 5 days and add it to the current date.

Python
future_date = now + datetime.[1](days=[2])
Drag options to blanks, or click blank then click option'
Atimedelta
B5
C10
Ddate
Attempts:
3 left
💡 Hint
Common Mistakes
Using date instead of timedelta.
Using wrong number of days like 10.
5fill in blank
hard

Fill both blanks to create a dictionary with keys as month names and values as the number of days, only for months with more than 30 days.

Python
months = {"January": 31, "February": 28, "April": 30, "May": 31}
long_months = {k: v for k, v in months.items() if v {BLANK_2}} {{BLANK_2}}
Drag options to blanks, or click blank then click option'
A:
B>
C30
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':' in the dictionary comprehension.
Using '<' or '=' instead of '>' in the condition.