Choose Your Own Pyventure/Midclass Review

REVIEW
Okay, it's not all review. Some of it is new. The rest of it is review.

These examples and questions should be challenging! Get in, and get dirty! The only way to learn to code is to try to make code. When it breaks:


 * take a deep breath
 * read the error message. They are often quite informative.
 * check your
 * indentation
 * matching parentheses / braces / brackets / quotes
 * ego. We all make mistakes!

If you're confused about anything here, seek out a more complete Python reference, or bring questions to class.

Dictionaries

 * dicts associate keys with values
 * are accessed through their keys
 * have no inherent order


 * Exercise: Create the dictionary D, which we'll use throughout the exercises. Note: we can use the dict function to make a dictionary as well.

What is broken here? Now try this.


 * what are the keys of D


 * what are the values?


 * try to access the values of D


 * try deleting a key-value pair from D


 * try adding a key-value pair to D: 'newkey' -> True


 * Examine the get method of dictionaries
 * try:
 * D.get('i',"notfound")
 * D.get('c',"notfound")
 * D.get('c',"notfound")


 * extra credit: try  dict(1='a').  Predict what will happen, and explain it.
 * dict(a=1)
 * dict('a'=1)
 * dict(class=1)
 * help(dict)


 * extra credit: What is the first element of D?

Lists, Tuples, Strings
List, tuples and strings are all python sequence types
 * they are ordered
 * they are accessed by their index [0,..,n], unlike dictionaries, which are accessed by key
 * some are mutable (meaning the elements can change)
 * strings have additional methods like upper, lower, split etc.
 * sequences support slicing (see below)


 * construct  like this:

L = [8,'a',[1,2,3],None,'b'] t = ('hallway', "a creepy hallway!") s = "a few of my favoirite strings"


 * try accessing different elements.

L[0] s[1] t[2] L['a']

L[0] = 1 s[0] = 1 t[0] = 1
 * which ones are immutable?


 * extra credit experiements in slicing

L[:2] L[2:] L[-3] L[10] L[::-1]

Functions

 * make this function:

yell_month(name,monthint) -> string

that works like this:

>>> yell_month('gregg', 1) 'January GREGG'

>>> yell_month('mIRANda', 7) 'July MIRANDA'


 * try it using a dictionary, and if/elif/else statements

Loops
Sometimes we want to do things more than once. Computers don't get bored by repetitive work, and have infinite patience. Exploit this!

Iterators
Python has a magic appearing construct called, that iterates over data structures.

for element in thing: print element, type(element)

Try iterating over our sequences, substituting our friends  for 'thing'.

While Loops
The while constructor runs the body of the loop for as long as the condition is True.

Here is an example:


 * try this with different values for n, like 0, 1, -1, 'a', None
 * write a function that counts up to a number.


 * final_countdown, which despite its name, uses a for loop

extra credit: make countdown wait
What if we actually want the function to wait one second between loops? There is a python function called [time.sleep] that we can use.

extra credit:  and

 * breaks out of a loop
 * goes on to the repeat in the loop

The Standard Library
Explore the standard library.
 * Choose a function or module, and experiment with it.
 * Tell the class which one you chose!
 * Show some code using it.
 * Describe its inputs and outputs.

Get Dirty!
Look at the standard exceptions (errors), and see which ones you recognize. Try to write code that triggers these errors:


 * SyntaxError
 * KeyError
 * NameError
 * TypeError (hint:   is a good candidate!)
 * IndexError
 * ImportError