What is the expected result of the following code?
from datetime import datetime datetime_1 = datetime(2019, 11, 27, 11, 27, 22) datetime_2 = datetime(2019, 11, 27, 0, 0, 0) print(datetime_1 - datetime_2)
- 11:27:22
- 11 hours, 27 minutes, 22 seconds
- 0 days
- 0 days, 11:27:22
Explanation: Let’s analyze the code:
- Line 1: the datetime function is imported from the datetime module.
- Line 3: a datetime_1 object is created and assigned the following value: 2019-11-27 11:27:22 returned as a result of the datetime(2019, 11, 27, 11, 27, 22) function operation.
- Line 4: a datetime_2 object is created and assigned the following value: 2019-11-27 00:00:00 returned as a result of the datetime(2019, 11, 27, 0, 0, 0) function operation.
- Line 6: it’s possible to perform arithmetic operations on datetime objects, and the result of the subtraction is returned as a timedelta object that expresses the difference in days between the two dates, which is printed to the console.
More Questions: Python Essentials 2 (PE2) Course Final Test
Please login or Register to submit your answer