Python ยท control flow

For Loops

Run the same block once for every item in a sequence: numbers, list items or characters.

Basic for loop


          # iterate over range
          for i in range(5):
              print(i)
          # ans: prints 0, 1, 2, 3, 4 (one per line)
        
range(5)

Each pass, i takes the next value from range(5); the loop stops when the values run out.

Range with start and stop


          # range(start, stop)
          for i in range(2, 7):
              print(i)
          # ans: 2, 3, 4, 5, 6
        
range(2, 7): the stop value 7 is not included

Range with a step


          # range(start, stop, step)
          for i in range(0, 10, 2):
              print(i)
          # ans: 0, 2, 4, 6, 8
        
range(0, 10, 2): count up in twos

How range() counts

start stop (excluded)

Start at start, add step each time, and stop before reaching stop.

Iterate over a list


          # iterate list items
          fruits = ["apple", "banana", "cherry"]
          for fruit in fruits:
              print(fruit)
          # ans: apple, banana, cherry (one per line)
        
fruits

No index needed: the loop variable is each item itself.

Iterate with an index


          # enumerate for index and value
          fruits = ["apple", "banana"]
          for i, fruit in enumerate(fruits):
              print(i, fruit)
          # ans: 0 apple
          # ans: 1 banana
        
enumerate(fruits) hands out (index, item) pairs

Iterate over a string


          # iterate over characters
          for char in "abc":
              print(char)
          # ans: a, b, c (one per line)
        
a string is a sequence of characters

Exercises

Predict the answer, then press โ†’ to trace the loop and check.

Exercise 1


          # how many iterations?
          for i in range(10):
              pass
          # ans: 10 iterations
        

Exercise 2


          # what is i after the loop?
          for i in range(3):
              pass
          # ans: i is 2 (the last value)
        

Exercise 3


          # what prints?
          for i in range(3, 3):
              print(i)
          # ans: nothing (empty range)
        

Exercise 4


          # negative step?
          for i in range(5, 0, -1):
              print(i)
          # ans: 5, 4, 3, 2, 1
        

Exercise 5


          # what is the sum?
          total = 0
          for i in range(1, 6):
              total += i
          # ans: 15 (1+2+3+4+5)
        

Exercise 6


          # enumerate start?
          for i, x in enumerate(['a', 'b'], start=1):
              print(i)
          # ans: 1, 2
        

Exercise 7


          # what is the final i?
          for i in range(5):
              if i == 3:
                  pass
          # ans: i is 4 (the loop continues)
        

Exercise 8


          # nested loop count?
          count = 0
          for i in range(3):
              for j in range(2):
                  count += 1
          # ans: count is 6
        

Exercise 9


          # empty list?
          for item in []:
              print(item)
          # ans: nothing (empty list)
        

Exercise 10


          # what prints?
          for i in range(1, 10, 3):
              print(i)
          # ans: 1, 4, 7
        

Exercise 11


          # modify the loop variable?
          for i in range(3):
              i = 10
              print(i)
          # ans: 10, 10, 10
          # ans: (it doesn't change the iteration)
        

Remember

range(stop)

Counts from 0 up to, but not including, stop.

range(start, stop, step)

A negative step counts down; an empty range runs the body zero times.

Any sequence

Lists, strings and enumerate(...) pairs all work in a for.

The loop variable

Keeps its last value after the loop; reassigning it inside does not change what comes next.