For loops and range()
Learn to iterate over lists and sequences with for and generate numbers with range().
What You'll Learn
- Use the for loop to iterate over lists and strings
- Generate number sequences with range()
In the last few lessons you drove every loop yourself with while: set a counter, check a condition, increment, repeat. It works, but it is like counting every student in the register by hand, line by line, until you hit the end. The for loop does the counting for you. You say "take each element from this collection and do something with it," and Python walks the collection on its own. No counter, no stop condition, no += 1 you can forget.
Here is the rule worth keeping: reach for while when you do not know when to stop (waiting on user input). Reach for for when you already know what you are walking through, a list of subjects, a set of grades, a weekly timetable. That is the whole job this lesson. We build a school schedule generator, piece by piece, and you will feel how much shorter the code gets.
We learn for and range() here with the school schedule generator, because the example is small and easy to read. Then, in the exercises, you take the same loops back to the dungeon: walk through the rooms one by one, collect the treasures, and fight the monsters in a row.
In this lesson you will be able to:
- Use a
forloop to walk through a list or a string, one element at a time - Generate number sequences with
range()using 1, 2, or 3 parameters - Know when to reach for
for(a known collection) versuswhile(a condition)
The for loop
The basic structure of a for loop is simple and straightforward:
for element in collection:
# do something with element
pass
The word element is a variable that takes on the value of each element in the collection, one at a time. You can name it anything you want. Python does not force a particular name. What matters is that the name makes sense in context.
Start with a simple example. You have a list of school days and you want to display each one:
Result:
Today is: Monday
Today is: Tuesday
Today is: Wednesday
Today is: Thursday
Today is: Friday
At each iteration (pass through the loop), the variable day receives the next value from the list. The first time it is "Monday", then "Tuesday", and so on until "Friday". When the list ends, the loop stops automatically. You do not need any counter or stopping condition.
You can also use for on a string. Python treats it as a collection of characters:
Result:
M
A
T
H
Iterating over lists
Now something more practical. Imagine you have a list of Monday's subjects and you want to display them numbered, like in a real timetable. You can use for together with a counter variable:
Result:
=== Monday Schedule ===
Period 1 - English
Period 2 - Mathematics
Period 3 - Physics
Period 4 - History
Period 5 - PE
We go through each subject in the list and, at the same time, keep a counter that increases by 1 at each step. Simple and clean.
You can combine for with if to filter elements. Say you want to see only the subjects that start with the letter "M":
Result:
Subjects starting with M:
- Mathematics
- Music
- Media Studies
The range() function
The for loop does not only work with lists. You can give it any sequence, and range() is the function that generates sequences of numbers. When you give it a single parameter, it generates numbers from 0 up to that number (without including it):
Result:
Class period: 0
Class period: 1
Class period: 2
Class period: 3
Class period: 4
Class period: 5
range(6) generates the numbers 0, 1, 2, 3, 4, 5. Six numbers in total, starting from 0. The last number (6) is not included, exactly like slicing.
Reach for this when you want to repeat something an exact number of times:
Result:
Generating schedule for Year 7
Generating schedule for Year 8
Generating schedule for Year 9
Generating schedule for Year 10
Generating schedule for Year 11
We added + 7 to year so we start from Year 7 instead of Year 0. That + 7 works, but it is a patch. There is a cleaner way: give range() a starting number too.
range() with two parameters
When you give range() two values, the first is the starting point and the second is the limit (excluded):
Result:
Period 1
Period 2
Period 3
Period 4
Period 5
Period 6
range(1, 7) generates: 1, 2, 3, 4, 5, 6. It starts at 1 and stops before 7. Much clearer than manually adding +1 everywhere.
Use this to generate a schedule with classroom numbers:
Result:
Prof. Johnson - Room 100
Prof. Smith - Room 101
Prof. Williams - Room 102
Prof. Brown - Room 103
Prof. Davis - Room 104
Here we used i as an index both to access elements from the teacher list and to calculate the room number.
range() with three parameters (step)
Optional deep-dive, skip if you just want the basics. The two-parameter range() already covers most loops.
The third parameter of range() is the step, how much to jump between numbers. By default, the step is 1, but you can change it:
Result:
Long breaks after periods:
- After period 1
- After period 3
- After period 5
range(1, 7, 2) generates: 1, 3, 5. It starts at 1, jumps by 2, and stops before 7.
You can also use a negative step to count backwards. This is useful when you want to display a countdown:
Result:
Class starts in:
5 ...
4 ...
3 ...
2 ...
1 ...
Off to class!
range(5, 0, -1) generates: 5, 4, 3, 2, 1. It starts at 5, decreases by 1, and stops before 0.
The for loop automatically iterates over each element in a collection (list, string, or range). The range() function generates sequences of numbers with 1, 2, or 3 parameters (stop, start/stop, start/stop/step).
A note before the exercises. The default is plain for item in collection: let the loop hand you each element directly, no range(len(...)) and no item[i]. You only reach for a number when you genuinely need the position, for example when you print "Room 1:", "Room 2:". In that one case, keep a small counter of your own: set number = 1 before the loop and do number = number + 1 at the end of each pass. That is the one place where a counter still earns its place.
Exercise : Dungeon with 5 rooms
Exercise : Treasure in each room
Exercise : Fight 3 monsters in a row
Exercise : End of dungeon report
Exercise : Multiplication table
Exercise : Bar chart with # characters
Complete game code: Python Kingdom
Your game now has a dungeon with multiple rooms. The hero explores room by room, fighting monsters and finding treasures:
Look at how long this file is getting. The same fight logic is copy-pasted for every monster, the same room print repeats over and over. That is the next problem to solve. In the next lesson you wrap repeated blocks like these into your own functions, name them once, and call them whenever you need them, so the game stops growing line by line.
Test Your Knowledge
Check how well you understood the lesson with these 3 questions.

