Aan de slagGa gratis aan de slag

Turning pairs of datetimes into durations

When working with timestamps, we often want to know how much time has elapsed between events. Thankfully, we can use datetime arithmetic to ask Python to do the heavy lifting for us so we don't need to worry about day, month, or year boundaries. Let's calculate the number of seconds that the bike was out of the dock for each trip.

Continuing our work from a previous coding exercise, the bike trip data has been loaded as the list onebike_datetimes. Each element of the list consists of two datetime objects, corresponding to the start and end of a trip, respectively.

Deze oefening maakt deel uit van de cursus

Working with Dates and Times in Python

Cursus bekijken

Oefeninstructies

  • Within the loop:
    • Use arithmetic on the start and end elements to find the length of the trip
    • Save the results to trip_duration.
    • Calculate trip_length_seconds from trip_duration.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Initialize a list for all the trip durations
onebike_durations = []

for trip in onebike_datetimes:
  # Create a timedelta object corresponding to the length of the trip
  trip_duration = ____[____] - ____[____]
  
  # Get the total elapsed seconds in trip_duration
  trip_length_seconds = trip_duration.____()
  
  # Append the results to our list
  onebike_durations.append(trip_length_seconds)
Code bewerken en uitvoeren