Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Time
4. The replace() function
Date
Datetime
Summary

Instruction

Very good! Suppose our plane is delayed by 15 minutes. How can we update our time object?

All datetime module objects are immutable. Once you create a time variable, you can't change any of its elements. What you can do is create a new time object based on an existing object. We do this using the replace() function. Take a look:

fra_mad_scheduled = datetime.time(9, 30, 0)
fra_mad_actual = fra_mad_scheduled.replace(minute=45)
print(fra_mad_actual)

The replace() function is used to copy an existing time object. Inside the parentheses, we specify the elements we want to replace: hour, minute, second, or microsecond. In this case, we only want to update the minute element.

Exercise

Look at the template code, and then use the replace() function to create a new time object named work_rare_start_time that should point to 8:30 AM. Then, print the following two lines:

Typical work start time: {work_start_time}
Rare work start time: {work_rare_start_time}

Stuck? Here's a hint!

You can use the following invocation:

work_start_time.replace(minute=30)