Python - Standard Library UsageYou want to calculate the number of days between two dates: April 1, 2024 and April 15, 2024. Which code correctly does this?Afrom datetime import date (d1 + d2).days where d1 = date(2024,4,1), d2 = date(2024,4,15)Bfrom datetime import datetime (d1 - d2).seconds where d1 = datetime(2024,4,1), d2 = datetime(2024,4,15)Cfrom datetime import date (d2 - d1).days where d1 = date(2024,4,1), d2 = date(2024,4,15)Dfrom datetime import timedelta timedelta(days=14).daysCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand date subtractionSubtracting two date objects returns a timedelta representing the difference.Step 2: Extract days from timedeltaAccess the .days attribute to get the number of days difference.Final Answer:Subtract dates and get .days attribute -> Option CQuick Check:Date subtraction returns timedelta; use .days [OK]Quick Trick: Subtract dates, then use .days to get difference [OK]Common Mistakes:MISTAKESSubtracting in wrong orderUsing .seconds instead of .daysAdding dates instead of subtracting
Master "Standard Library Usage" in Python9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Python Quizzes Advanced Exception Handling - Try–except–finally behavior - Quiz 8hard Context Managers - Handling multiple resources - Quiz 9hard Encapsulation and Data Protection - Private attributes - Quiz 6medium Exception Handling Fundamentals - Why exceptions occur - Quiz 5medium Exception Handling Fundamentals - Generic exception handling - Quiz 14medium Exception Handling Fundamentals - Generic exception handling - Quiz 3easy File Handling Fundamentals - Opening and closing files - Quiz 6medium File Handling Fundamentals - Appending data to files - Quiz 8hard File Handling Fundamentals - Writing file data - Quiz 9hard Modules and Code Organization - __init__ file role - Quiz 8hard