Breakout 4 : Functions and Modules

In the breakout, we should do this work in a files called age.py rather than in the notebook. However, if you are more comfortable in the notebook, the solution to the first part of the problem can be found in this notebook.

First, we want to import datetime, which is a python module for dates and times and such.

In [ ]:
import datetime

Next, we want to use datetime.datetime() to create a variable representing when John Cleese was born.

Note that utcnow() gives the universal time, while .now() gives the local time. We're ignoring timezone stuff here.

In [ ]:
born = datetime.datetime(1939, 10, 27)

Then use datetime.datetime.now() to create a variable representing now.

In [ ]:
now = datetime.datetime.now()

Next, subtract the two, forming a new variable, which will be a datetime.timedelta() object.

In [ ]:
cleese_age = now - born

Finally, print that variable.

In [ ]:
print(cleese_age)

Grab just the days :

In [ ]:
print("days John Cleese has been alive : ", cleese_age.days)

There is no hours data member, so let's multiply to find the hours :

In [ ]:
print("hours John Cleese has been alive : ", cleese_age.days * 24)

What will be the date in 1000 days from now?

In [ ]:
td = datetime.timedelta(days=1000)

Print it.

In [ ]:
print("in 1000 days it will be ",
      # this is a datetime object
      now + td)
In [ ]: