{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Breakout 4 : Functions and Modules\n", "====================================\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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. \n", "\n", "First, we want to import datetime, which is a python module for dates and times and such. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import datetime" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we want to use datetime.datetime() to create a variable representing when John Cleese was born.\n", "\n", "Note that utcnow() gives the universal time, while .now() gives the local time. We're ignoring timezone stuff here." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "born = datetime.datetime(1939, 10, 27)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then use datetime.datetime.now() to create a variable representing now.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "now = datetime.datetime.now()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, subtract the two, forming a new variable, which will be a datetime.timedelta() object." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cleese_age = now - born" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, print that variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(cleese_age)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Grab just the days : " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"days John Cleese has been alive : \", cleese_age.days)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is no hours data member, so let's multiply to find the hours : " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"hours John Cleese has been alive : \", cleese_age.days * 24)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What will be the date in 1000 days from now?\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "td = datetime.timedelta(days=1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Print it." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"in 1000 days it will be \",\n", " # this is a datetime object\n", " now + td)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }