Boston Python Workshop 7/Wordplay handout
Wordplay goals
- practice for loops
- practice using lists
- practice manipulating strings
- get experience with regular expressions
- have fun cheating at crosswords and Words with Friends
Concept review
Indentation reminder
In Python, indentation matters. Everything is indented by a multiple of some number of spaces, often 4.
In if
statements, you indent everything you want to be run if the if
conditional is True
. 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 if
conditional is True
,
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 if
conditional is False
, 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 print
line is indented 4 spaces under the for
. That's how
Python knows to execute the print line for every name in names.
range
>>> range(5) [0, 1, 2, 3, 4] >>> for i in range(5): ... print "Hi" * i ... Hi HiHi HiHiHi HiHiHiHi
if
statements inside for
loops
>>> for name in ["Alice", "Bob", "Cassie", "Deb", "Ellen"]: ... if name[0] in "AEIOU": ... print name + " starts with a vowel." ... Alice starts with a vowel. Ellen starts with a vowel.
for
loops inside for
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, random
and time
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
random
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
randint
is a function in the random
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.
New Wordplay material summary
in
keyword
in is a keyword checking for containment. You can use it in a couple
of ways:
>>> "a" in "apple"
True
>>> "z" in "apple"
False
>>> dogs = ["pug", "boxer", "dalmation"]
>>> "tiger" in dogs
False
>>> "pug" in dogs
True
Reversing lists
Here's a quick way to reverse a list.
>>> fruits = ["apples", "bananas", "cherries"]
>>> fruits[::-1]
['cherries', 'bananas', 'apples']
Let's break down why this works:
First, remember that we can get individual elements from lists:
>>> fruits[0]
'apples'
We can also slice lists:
>>> fruits[0:2]
['apples', 'bananas']
>>> fruits[:2]
['apples', 'bananas']
>>> fruits[1:]
['bananas', 'cherries']
We can also make a copy of the list by taking a slice from the
beginning to the end of the list:
>>> my_fruits = fruits[:]
['apples', 'bananas', 'cherries']
There's an extended slicing syntax that let's you say what direction
you want to slice in. By default it is forward, but you can supply a
-1
to say backwards:
>>> fruits[::-1]
['cherries', 'bananas', 'apples']
Regular expressions
- ^ is an anchor that means "beginning"
- $ is an anchor that means "end"
- . means a single wildcard character
- .* means any number (including zero) of wildcard characters
Examples:
rstu
will match anything that contains "rstu", for example "understudy".
^aa
will match anything that starts with "aa", for example "aardvark".
a.b.c
will match anything containing "a", then any single character, then "b", then any single character, then "c", for example "iambic".
ss.*ss
will match anything containing "ss", then anything, then "ss", for example "messiness".
^c.d.e$
will match anything that starts with "c", then any single character, then d, then any single character, then ends with an "e", for example "cadre".