
Starting Your PyCon Countdown
Starting Your PyCon Countdown 관련


Now you have enough information to start working on a countdown clock for next year’s PyCon US! PyCon US 2021 will start on May 12, 2021 in Pittsburgh, PA. With the 2020 event having been canceled, many Pythonistas are extra excited for next year’s gathering. This is a great way to keep track of how long you’ll need to wait and boost your datetime
skills at the same time!
To get started, create a file called pyconcd.py
and add this code:
from datetime import datetime
PYCON_DATE = datetime(year=2021, month=5, day=12, hour=8)
countdown = PYCON_DATE - datetime.now()
print(f"Countdown to PyCon US 2021: {countdown}")
In this code, you import datetime
from datetime
and define a constant, PYCON_DATE
, that stores the date of the next PyCon US. You don’t expect the date of PyCon to change, so you name the variable in all caps to indicate that it’s a constant.
Next, you compute the difference between datetime.now()
, which is the current time, and PYCON_DATE
. Taking the difference between two datetime
instances returns a datetime.timedelta
instance.
timedelta
instances represent the change in time between two datetime
instances. The delta in the name is a reference to the Greek letter delta, which is used in science and engineering to mean a change. You’ll learn more later about how to use timedelta
for more general arithmetic operations.
Finally the printed output, as of April 9, 2020 at a little before 9:30 PM is:
Countdown to PyCon US 2021: 397 days, 10:35:32.139350
Only 397 days until PyCon US 2021! This output is a little clunky, so later on you’ll see how you can improve the formatting. If you run this script on a different day, you’ll get a different output. If you run the script after May 12, 2021 at 8:00 AM, you’ll get a negative amount of time remaining!