- Identify when loops should be used to avoid manual repitition
- Select the best loop type for a given situation
- Implement a clear usage of loop variables and loop conditions
- Construct clear and concise conditional expressions
- Use comprehensions for simpler and readable code
- Invoke exceptions for error handling
- Write a function that returns one or more variables
Chatper 4, pages 77-93 Chapter 5, pages 95-98
-
Loops - Don't repeat yourself (115)
- basic syntax: while vs for
- when to use each one
- for: "counting" up to fixed length known a priori
- while: arbitrary condition, unknown length, possibly not "counting"
- when to use each one
- for loop patterns:
- basic: item in list
- counting: index in range(N)
- counting with list: (index, item) in enumerate(list)
- working with parallel list: (item1, item2) in zip(list1, list2)
- while loop patterns: a. initialize condition b. while condition c. do things including possibly changing condition
- keeping track of variables inside and outside loops
- basic syntax: while vs for
-
Comprehensions (120)
- ideal when building a new container by looping through an existing one
- A = transform(B)
-
Conditionals (107)
- basic syntax - note indentation!
- special operators:
in
,not in
,is
,is not
- conditional expressions
- compound conditions & operator precedence
-
Comprehensions revisited (122)
- A = filter(B)
-
Exceptions (112)
- error handling
- a way to communicate errors without passing around error codes
- not a substitute for full implementation
- basic syntax: try ... except (e.g. error msg)
- catching specific exceptions
- raising exceptions
-
Functions (126)
- syntax
- scope