日期运算

在 Python 中处理日期和时间

Max Shron

Data Scientist and Author

日期运算

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

# Find the least least in the list print(min(l)) 11
在 Python 中处理日期和时间

日期运算

# Subtract two numbers
print(b - a)
3

# Add 3 to a
print(a + 3)
14
在 Python 中处理日期和时间

日期运算

在 Python 中处理日期和时间

日期运算

# 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
在 Python 中处理日期和时间

日期运算

# Subtract two dates
delta = d2 - d1

print(delta.days)
29
在 Python 中处理日期和时间

日期运算

# Import timedelta
from datetime import timedelta

# Create a 29 day timedelta td = timedelta(days=29) print(d1 + td) 2017-12-04
在 Python 中处理日期和时间

用 += 递增变量

# 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
在 Python 中处理日期和时间

Let's Practice!

在 Python 中处理日期和时间

Preparing Video For Download...