More Iteration Preparation
Overview
Simple definite (for) loops are great when you know exactly how many times you want to iterate. But there are situations where you just don’t know how long the loop should run, like when you are relying on input from the user to tell you they are done with something. Sometimes you also want to be able to work with 2D data, such as matrices or grids and to iterate over them, you need a more complex structure than your basic definite loop. Today we’ll be exploring some variations on iteration that let us do more complex tasks.
Basic Learning Objectives
Before class, you should be able to:
- Write a basic indefinite loop using
while - Predict what output a nested loop will produce
- Identify what value is at a given index location of a nested list
Advanced Learning Objectives
After class, you should be able to:
- Write an indefinite loop to solve a problem
- Write a nested loop to produce specific output
- Use a nested list to solve a problem
Resources
You should read/watch the following:
- The
whilestatement - The Listener Loop
breakandcontinue- Nested Data - stop when you get to discussion of “dictionaries”
- Nested Iteration
Check
Submit answers to the following on Moodle:
-
Rewrite the following for loop as a while loop, keeping the output the same:
- What will the following output?
for i in range(3): for j in ['a', 'b']: print(i, j) - Given the following nested list:
my_list = [[1, 'a'], [5, 'c'], ['z', 10], [100, 200, 300]]What is the value at
my_list[1][0]?