python-for-j277 / control-flow · Lesson 08
Lesson 08 — Control flow
Iteration: For Loops with range()
Use range() to supply numbers to the same for loop.
range() is placed after in when a for loop needs a sequence of integers. The loop still follows exactly the same pattern: for variable in values:.
Not a second type of for loop
for name in names: and for number in range(5): are the same kind of Python loop. Only the source of the values is different.Same loop, different source
names = ["Ava", "Ben", "Cara"]
for name in names:
print(name)
for number in range(3):
print(number)Output
Ava Ben Cara 0 1 2
How it works
- In the first loop,
namessupplies three strings. - In the second loop,
range(3)supplies three integers: 0, 1 and 2. - Both loops take one value at a time and store it in the loop variable.
range()changes the values being supplied, not the type of loop.
Repeating a fixed number of times
for repeat in range(5):
print("Hello")Output
Hello Hello Hello Hello Hello
How it works
range(5)supplies five values: 0, 1, 2, 3 and 4.- The loop therefore runs five times.
- The program does not use
repeatinside the loop; the values simply control how many passes occur.
Using the number supplied by range()
for number in range(5):
print("Number", number)Output
Number 0 Number 1 Number 2 Number 3 Number 4
How it works
numberreceives each integer supplied byrange(5).range(5)starts at 0 and stops before 5.- Use the loop variable when the number itself is needed in the calculation or output.
Choosing the start and stop values
for number in range(1, 6):
print(number)Output
1 2 3 4 5
How it works
- The first value, 1, is the starting value and is included.
- The second value, 6, is the stopping boundary and is not included.
range(1, 6)therefore supplies 1, 2, 3, 4 and 5.
The stop value is excluded
range() stops before its stop value. To produce the numbers 1 to 5, write range(1, 6).Counting in steps
for number in range(0, 11, 2):
print(number)
for number in range(5, 0, -1):
print(number)Output
0 2 4 6 8 10 5 4 3 2 1
How it works
- The third value is the step: how much the number changes after each pass.
range(0, 11, 2)counts upwards in twos.range(5, 0, -1)counts downwards because the step is negative.- The stop boundary is still excluded in both examples.
Using range() for list indexes
Use indexes only when the position of each item is needed. If you only need the items, loop through the list directly.
names = ["Ava", "Ben", "Cara"]
for index in range(len(names)):
print(index, names[index])Output
0 Ava 1 Ben 2 Cara
How it works
len(names)is 3, sorange(len(names))supplies 0, 1 and 2.- Those are the valid indexes of the three list items.
names[index]accesses the item at the current position.- This is still the same
for variable in values:loop pattern.
Do not use an index unless you need itWrite
for name in names: when only the names are needed. Write for index in range(len(names)): when the position is also needed.Choose the values needed after in
| What the task needs | Values supplied after in | Code |
|---|---|---|
| Every item in a list | The list items | for name in names: |
| Exactly five repeats | Five integers | for repeat in range(5): |
| The numbers 1 to 5 | Integers 1, 2, 3, 4, 5 | for number in range(1, 6): |
| Every list position | Valid indexes | for index in range(len(names)): |
What you have learned
range()supplies integer values to a normalforloop.range(n)supplies 0 up ton - 1.range(start, stop)includes the start but excludes the stop.range(start, stop, step)changes by the given step.- Use
range(len(list_name))only when indexes are needed. - “For-each” and “count-controlled” describe purposes, not separate Python
forstatements.