Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Standard Library Usage
What will be the output of this code?
from datetime import datetime, timedelta
now = datetime(2024, 4, 10, 12, 0)
later = now + timedelta(days=5, hours=3)
print(later)
A2024-04-15 15:00:00
B2024-04-15 03:00:00
C2024-04-05 15:00:00
D2024-04-10 15:00:00
Step-by-Step Solution
Solution:
  1. Step 1: Calculate the new date by adding timedelta

    Starting from April 10, 2024, 12:00, add 5 days and 3 hours.
  2. Step 2: Perform the addition

    Adding 5 days: April 15, 2024; adding 3 hours: 15:00 (3 PM).
  3. Final Answer:

    2024-04-15 15:00:00 -> Option A
  4. Quick Check:

    Adding timedelta updates date and time correctly [OK]
Quick Trick: timedelta adds days and hours correctly to datetime [OK]
Common Mistakes:
  • Adding hours incorrectly
  • Miscounting days
  • Confusing date and time parts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes