Week 3: Exceptions, recursion, and nested loops#
- Errors that end your program early
- Syntax errors
- Name/variable binding errors
- Type errors
- Errors from partial operations
- Interruptions from the user and the environment
- What kind of error?
- Try/except statements
- Exceptional control flow
- Breakfast
- Stack traces
- Catching only certain exceptions
- Hardened square-root program
- Raising exceptions
- Exceptional control flow, redux
- Catching errors early with assert
- Testing types with isinstance
- isinstance
- Surprise: integers and floats are unrelated
- Surprise: bools are numbers!
- Recursion: functions that call themselves
- Identifying the parts
- Sum and length
- Visualizing recursive call stacks
- Concatenating nested strings
- Recursion into iteration (and vice versa)
- What can’t a single for loop do?
- Nested for loops
- Simulating nested for loops
- Iterate through lists using inner for-loops
- More simulation
- Predicting COVID cases
- Doing your times tables
- [OPTIONAL] Prime numbers
- Lists of lists
- Making a 2D list
- Indexing nested lists
- Nested lists
- Nested for loops and 2D lists
- increment_2D
- What is an image?
- Images and 2D lists
- Pixel numbering
- Pixels, Color and RGB
- Color quiz
- Tuples
- create_pixel
- Tuples and 2D lists
- red_square
- Recap
- Accessing rows of images
- Getting a row
- Accessing columns of images
- Getting a column
- Nested for loops to access images
- Getting a column
- Modifying pixels of an image
- Modifying pixels using for loops
- More for loops
- Blacking out checkers
- Apply a red filter to an image
- red_filter_pixel
- Using for loops
- Red filter
- Working in just a region of an image
- Red shifted region
- Customizing red_filter
- Thinking about custom_red_filter
- custom_blue_filter
- Handling edge cases
- Bad inputs
- custom_blue_filter, revisited
- Flipping images left-to-right
- Flipping cats
- Flipping with nested for loops
- More of a flop than a flip
- List swap
- We’re all tired at this point in the semester
- Defining a function for flip
- red_filter, in place
- Returning an image
- Understanding objects and references
- Creating a copy of an image
- Copying and flipping
- Copies and references
- Overwriting references
- Overwriting references
- red_filter, copying