Iteration
Iteration is the name given to making code run more than one time. There are two key types of iteration:
- For loops
- While loops
for X in Y
The for X in Y loop can be used with any iterable. An iterable is a data type we can move through one item after the next. We can go character by character through
a string or item by item through a list or dictionary.
The X in a for X in Y loop is a temporary variable that stores the first value and then the second and then the third etc until it reaches the end.
word = "computer" for letter in word: print(letter) |
In the code above the X is letter. The first go through the loop letter holds the value 'c', next it is an 'o', then a 'm' etc. The loop ends when there are no more
characters to iterate through. The Y in the code above is the variable word which stores a string we can iterate through.
for X in Y syntax rules:
- for is in lower case
- you specify a valid name for the temporary valuable
- You use the
'in' keyword to state which iterable to look in
- There is a colon on the end of the for line
- Any code you want to be executed each time through the loop
is indented under the for loop
Syntax checker
Fix the code below so it counts and outputs the number of instances of the letter s are in a persons name they entered.
name = input("Enter your name: "): |
num_s = 0 |
for letter in name: |
if letter == "s": |
num_s = num_s + 1 |
print(f"There are {num_s} instances of the letter s in your name.") |
for i in range()
In a for i in range loop we create our own iterator. This is a variable that holds a number that goes up by 1 each time through the loop. We need to pass at least one value to
a for i in range loop to tell it how many times to run but have the option to pass it more than one value.
for i in range with one parameter
When we specify one value the iterator starts with a value of 0 and goes until the iterator is one less than the specified number.
for i in range(5): print(f"This time through the loop the iterator is {i}") |
The code above will run the loop 5 times in total, but the value of the iterator never reaches 5. The last line output would be 'This time through the loop the iterator is 4'.
for i in range with two parameters
When we specify two values the iterator starts at the first value and goes until the iterator is one less than the specified number.
for i in range(5,8): print(f"This time through the loop the iterator is {i}") |
The code above will run the loop 3 times in total. The outputs would be:
- 'This time through the loop the iterator is 5'.
- 'This time through the loop the iterator is 6'.
- 'This time through the loop
the iterator is 7'.
for i in range with three parameters
When we specify three values the iterator starts at the first value and goes until the iterator is one less than the specified number, but it increments by the 3rd value each time.
for i in range(0,100,5): print(f"This time through the loop the iterator is {i}") |
The code above will run the loop 20 times in total. The outputs would be:
- 'This time through the loop the iterator is 0'.
- 'This time through the loop the iterator is 5'.
- ...this pattern continues until...
- 'This time through the loop the iterator is 95'.
for i in range syntax rules:
- for i in range( ) is in lower case
- A letter or variable is used to temporarily store the iterator while the loop
runs, normally I the first time
- At least one value is specified in the brackets after range
- There is a colon on the end of the for line
- Anything you want to happen
each time the loop runs is indented under the for line
Syntax checker
Fix the code below so it counts and outputs the reuested numbers.
for i in range(1,21): |
print(i) |
for i in range(5,11): |
print(i) |
for i in range(100,0,-1): |
print(i) |