Boston Python Workshop 5/ColorWall handout

ColorWall goals

 * practice for loops
 * practice using functions
 * practice implementing functions
 * have fun experimenting with Python code that produces graphical results

Indentation reminder
In Python, indentation matters. Everything is indented by a multiple of some number of spaces, often 4.

In  statements, you indent everything you want to be run if the if conditional is. For example:

>>> James = 35 >>> Alice = 30 >>> if James > Alice: ...    print "James is older than Alice." ... James is older than Alice. >>>

Because James really is older than Alice, the  conditional is  , so Python does execute the code indented under the if line. In this case we print "James is older than Alice."

>>> James = 35 >>> Alice = 30 >>> if James < Alice: ...    print "James is younger than Alice." ... >>>

Because James is not older than Alice, the  conditional is , so Python does not execute the code indented under the if line.

In for loops, you indent everything you want to be run each loop For example:

>>> names = ["Jessica", "Adam", "Liz"] >>> for name in names: ...    print "Hello", name ... Hello Jessica Hello Adam Hello Liz

The  line is indented 4 spaces under the. That's how Python knows to execute the print line for every name in names.

dictionaries

 * dictionaries are a way to map keys to values
 * if you used a Python dictionary to store the information from a real dictionary, the keys would be words and the values would be definitions
 * dictionaries aren't ordered (lists are ordered)

>>> flavors = {"Jessica" : "Chocolate", "Adam" : "Vanilla", "Liz" : "Strawberry"} >>> flavors["Jessica"] 'Chocolate' >>> flavors["Eve"] Traceback (most recent call last): File " ", line 1, in KeyError: 'Eve' >>> flavors.get("Eve") >>> flavors["Eve"] = "Rocky Road" >>> flavors {'Eve': 'Rocky Road', 'Jessica': 'Chocolate', 'Liz': 'Strawberry', 'Adam': 'Vanilla'}

getting keys and values from dictionaries

 * use keys to get a list of keys
 * use values to get a list of values

>>> flavors = {"Jessica" : "Chocolate", "Adam" : "Vanilla", "Liz" : "Strawberry"} >>> for person in flavors.keys: ...    print person ... Jessica Liz Adam >>> for flavor in flavors.values: ...    print flavor ... Chocolate Strawberry Vanilla

range
>>> range(5) [0, 1, 2, 3, 4] >>> for i in range(5): ...    print "Hi" * i ...

Hi HiHi HiHiHi HiHiHiHi

statements inside loops
>>> for i in range(80): ...    if i % 9 == 0: ...        print i, "is divisible by 9." ... 0 is divisible by 9. 9 is divisible by 9. 18 is divisible by 9. 27 is divisible by 9. 36 is divisible by 9. 45 is divisible by 9. 54 is divisible by 9. 63 is divisible by 9. 72 is divisible by 9.

loops inside loops
>>> letters = ["a", "b", "c"] >>> numbers = [1, 2, 3] >>> for letter in letters: ...    for number in numbers: ...        print letter * number ... a aa aaa b bb bbb c cc ccc >>> for number in numbers: ...    for letter in letters: ...        print number * letter ... a b c aa bb cc aaa bbb ccc

Imports
Imports look like this:

>>> import random >>> import time

In the above example,  and   are both Python modules. Modules are Python files outside of the current Python file that contain Python code, like functions and variables. You can use code from modules by first importing the module. Here's an example from the module:

>>> import random >>> random.randint(0, 10) 7 >>> random.randint(0, 10) 6 >>> random.randint(0, 10) 1 >>> random.randint(0, 10) 3 >>> random.randint(0, 10) 4 >>> random.randint(0, 10) 9

is a function in the  module. It takes a lower bound as the first argument and an upper bound as the second argument and returns a random integer between those bounds.