Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Exercise

  • Let's create a file called birthday.py

BIRTHDAY = "1982-09-08"
print(BIRTHDAY)
  • Always run your script after each exercise!

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Exercise

  • Change birthday.py to look like this:

BIRTHDAY = "1982-09-08"
message = "I am born on {}".format(BIRTHDAY)
print(message)

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Exercise

  • With this new knowledge we can extend birthday.py a bit...
  • Let's make it so that we pass in the date via the command line
  • Make it look like this:

import sys

BIRTHDAY = sys.argv[1]
message = "I am born on {}".format(BIRTHDAY)
print(message)
  • Try to press the "Run" button

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Exercise

  • Let's build some error handling into our birthday.py

import sys

if len(sys.argv) < 2:
    print("ERROR: Please provide a date")
else:
    BIRTHDAY = sys.argv[1]
    message = "I am born on {}".format(BIRTHDAY)
    print(message)
  • By the way: "len" is a built-in function to return the length of a list
  • ...and "sys.argv" is just a list of command line arguments
  • The first argument is always the Python script filename
  • The second argument is supposed to be birthdate

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Exercise

  • Let's convert the birthdate input string into a datetime object

import sys
from datetime import datetime

if len(sys.argv) < 2:
    print("ERROR: Please provide a date")
else:
    BIRTHDAY = datetime.strptime(sys.argv[1], "%Y-%m-%d")
    message = "I am born on {}".format(BIRTHDAY.date())
    print(message)

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Exercise

  • Let's make sure that the date is formatted correctly:

import sys
from datetime import datetime

if len(sys.argv) < 2:
    print("ERROR: Please provide a date")
else:
    try:
        BIRTHDAY = datetime.strptime(sys.argv[1], "%Y-%m-%d")
    except ValueError:
        print("ERROR: Please enter a correct date")
        sys.exit()
    message = "I am born on {}".format(BIRTHDAY.date())
    print(message)
  • By the way: "sys.exit()" stops the program

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Exercise

  • Let's create a function that adds some years to a given date

import sys
from datetime import datetime, date

def get_future_date(base_date, years):
    future_date = date(
        base_date.year + years,
        base_date.month,
        base_date.day
    )
    return future_date

if len(sys.argv) < 3:
    print("ERROR: Please provide a date and an age")
else:
    try:
        birthday = datetime.strptime(sys.argv[1], "%Y-%m-%d")
    except ValueError:
        print("ERROR: Please enter a correct date")
        sys.exit()
    years = int(sys.argv[2])
    future_date = get_future_date(birthday, years)
    message = "I will turn {} on {}".format(years, future_date)
    print(message)

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Loops

  • You often need to repeat the same code in a loop
  • Loops look like this:

var my_list = [1, 2, 3, 4]

for my_number in my_list:
    if my_number == 1:
        continue
    print (my_number)
  • You can use "continue" to skip this iteration of the loop

Exercise

  • Let's extend the output of our program so that it shows all future birthdays up to the provided age

import sys
from datetime import datetime, date

def get_future_date(base_date, years):
    future_date = date(
        base_date.year + years,
        base_date.month,
        base_date.day
    )
    return future_date

if len(sys.argv) < 3:
    print("ERROR: Please provide a date and an age")
else:
    try:
        birthday = datetime.strptime(sys.argv[1], "%Y-%m-%d")
    except ValueError:
        print("ERROR: Please enter a correct date")
        sys.exit()
    years = int(sys.argv[2])
    years_list = range(1, years + 1, 1)
    for year in years_list:
        future_date = get_future_date(birthday, year)
        message = "I will turn {} on {}".format(year, future_date)
        print(message)

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?

Dictionaries

  • Dictionaries are so called "key-value-stores"
  • You can define an empty dictionary like this:
    my_dict = {}
  • You can also define it with initial values:
my_dict = {
    "Martin": 33,
    "Eva": 24,
}
  • You can access an item by it's key:
    martins_age = my_dict["Martin"]

Exercise

  • Let's add the final piece to our program
  • We will write a function that returns the weekday name for a given weekday number

import sys
from datetime import datetime, date

def get_weekday_name(weekday_number):
    weekdays = {
        1: "Monday",
        2: "Tuesday",
        3: "Wednesday",
        4: "Thursday",
        5: "Friday",
        6: "Saturday",
        7: "Sunday",
    }
    return weekdays[weekday_number]

def get_future_date(base_date, years):
    future_date = date(
        base_date.year + years,
        base_date.month,
        base_date.day
    )
    return future_date

if len(sys.argv) < 3:
    print("ERROR: Please provide a date and an age")
else:
    try:
        birthday = datetime.strptime(sys.argv[1], "%Y-%m-%d")
    except ValueError:
        print("ERROR: Please enter a correct date")
        sys.exit()
    years = int(sys.argv[2])
    years_list = range(1, years + 1, 1)
    for year in years_list:
        future_date = get_future_date(birthday, year)
        weekday = get_weekday_name(future_date.isoweekday())
        message = "I will turn {} on {}, {}".format(year, weekday, future_date)
        print(message)

Table of Content

  • Introduction
  • Python In A Nutshell
  • Cloud9 IDE
  • Variables
  • Strings
  • Input & Imports
  • The If-Statement & Type-casting
  • Datetimes
  • Exception Handling
  • Functions
  • Lists & Loops
  • Dictionaries
  • What's Next?