Math with Dates

Working with Dates and Times in Python

Max Shron

Data Scientist and Author

Math with dates

# Example numbers
a = 11
b = 14
l = [a, b]

# Find the least least in the list print(min(l)) 11
Working with Dates and Times in Python

Math with dates

# Subtract two numbers
print(b - a)
3

# Add 3 to a
print(a + 3)
14
Working with Dates and Times in Python

Math with dates

Working with Dates and Times in Python

Math with dates

# Import date
from datetime import date

# Create our dates d1 = date(2017, 11, 5) d2 = date(2017, 12, 4) l = [d1, d2]
print(min(l)) 2017-11-05
Working with Dates and Times in Python

Math with dates

# Subtract two dates
delta = d2 - d1

print(delta.days)
29
Working with Dates and Times in Python

Math with dates

# Import timedelta
from datetime import timedelta

# Create a 29 day timedelta td = timedelta(days=29) print(d1 + td) 2017-12-04
Working with Dates and Times in Python

Incrementing variables with +=

# Initialize x to be zero
x = 0

# Increment x
x = x + 1

print(x)
1
# Initialize x to be zero
x = 0

# Increment x
x += 1

print(x)
1
Working with Dates and Times in Python

Let's Practice!

Working with Dates and Times in Python

Preparing Video For Download...