Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Time
2. Creating time variables
Date
Datetime
Summary

Instruction

Let's get started! To use date and/or time data in Python, we have to import the datetime module first, like this:

import datetime

Now, let's define our first time variable. We'll be dealing with flight connections, and we want to define the scheduled time of departure for a flight from Frankfurt Airport (FRA) to Madrid Airport (MAD):

fra_mad_scheduled = datetime.time(9, 30, 0)
print(fra_mad_scheduled)

Result: 09:30:00

By invoking datetime.time(9, 30, 0), we created a new time object with the following arguments: hour=9, minute=30, second=0. Our fra_mad_scheduled variable now points to exactly 9:30 AM, but it is not associated with any specific date or time zone.

Remember that all time and date objects from the datetime module use the 24-hour clock. To show a 9:30 PM departure, use:

fra_mad_scheduled = datetime.time(21, 30, 0)

Exercise

Create a time object named work_start_time that points to 8:45 AM. Then, print the following:

Typical work start time is {time}

Remember to import the datetime module first.

Stuck? Here's a hint!

Use:

work_start_time = datetime.time(8, 45, 0)