0
0
Data Analysis Pythondata~10 mins

Date arithmetic (Timedelta) in Data Analysis 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 create a timedelta of 5 days.

Data Analysis Python
from datetime import timedelta

five_days = timedelta([1]=5)
print(five_days)
Drag options to blanks, or click blank then click option'
Adays
Bhours
Cminutes
Dseconds
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'day' instead of 'days' as the parameter name.
Using 'hours' or 'minutes' when the question asks for days.
2fill in blank
medium

Complete the code to calculate the date 10 days after January 1, 2024.

Data Analysis Python
from datetime import datetime, timedelta

start_date = datetime(2024, 1, 1)
new_date = start_date + timedelta([1]=10)
print(new_date.strftime('%Y-%m-%d'))
Drag options to blanks, or click blank then click option'
Adays
Bhours
Cminutes
Dseconds
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'day' instead of 'days'.
Using 'hours' or 'minutes' which changes the time, not the date.
3fill in blank
hard

Fix the error in the code to subtract 3 hours from a datetime object.

Data Analysis Python
from datetime import datetime, timedelta

now = datetime(2024, 6, 15, 12, 0)
new_time = now - timedelta([1]=3)
print(new_time.strftime('%H:%M'))
Drag options to blanks, or click blank then click option'
Adays
Bhours
Chour
Dminute
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hour' instead of 'hours'.
Using 'minute' when subtracting hours.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths if length is greater than 4.

Data Analysis Python
words = ['apple', 'bat', 'grape', 'kiwi']
lengths = {word: [1] for word in words if len(word) [2] 4}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of its length.
Using '<' instead of '>' in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths if length is greater than 3.

Data Analysis Python
words = ['dog', 'elephant', 'cat', 'lion']
result = { [1]: [2] for word in words if len(word) [3] 3 }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word as key instead of uppercase.
Using '<' instead of '>' in the condition.