# 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
get all keys:
* what are the values?
get values:
* try to access the values of D
Accessing:
* try deleting a key-value pair from D
delete using del
* try adding a key-value pair to D: 'newkey' -> True
adding:
* Examine the get method of dictionaries
** help(D.get)
** try:
*** D.get('i',"notfound")
*** D.get('c',"notfound")
Help on built-in function get:
Bascially, return D[k] if k is in D, otherwise, return d. This saves our bacon from having a KeyError when a key isn't in the dictionary.
* extra credit: try dict(1='a'). Predict what will happen, and explain it.
** dict(a=1)
** dict('a'=1)
** dict(class=1)
** help(dict)
1 isn't a valid python variable name. The dict function expects valid python identifiers, as described in the help.
* extra credit: What is the first element of D?
Trick question! Dictionaries don't have inherent order! It may vary from machine to machine or from python version to python version. In any case, it's not something to rely on.

### 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 L,t,s like this:
* try accessing different elements.
L['a'] fails because sequences are accessed by index not by name.
* which ones are immutable?
Tuples and Strings are immutable. Tuples imply that the positions mean something, where lists can be appended to, deleted from, and the like.
* extra credit experiements in slicing

### Functions

* make this function:
that works like this:
* try it using a dictionary, and if/elif/else statements
two methods

### 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 for...in, that iterates over data structures.
Try iterating over our sequences, substituting our friends D,s,t,L for 'thing'.
iterator example

#### 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: break and continue

* break breaks out of a loop
* continue goes on to the repeat in the loop
example with continue and break:
* try this out with various input sentences, to discover what it does

### 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: int is a good candidate!)
* IndexError
* ImportError
Generate some errors: