🐍 Python for kids · Class 7 · Lesson 10 of 18 · Free
enumerate() and range() – Smarter Counting Loops
⏱ About 25 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Use range() with start, stop and step, including counting backwards.
- Get the index and the item together with enumerate().
- Choose between enumerate(), range(len()) and a plain for loop.
At a school race, the announcer says "1st place, Riya" and "2nd place, Kabir". Your program needs both the position and the name. enumerate() gives you both in one go.
How does range() work with start, stop and step?
range() can take three numbers: where to start, where to stop and how big each jump is.
The stop number is never included. range(1, 11) gives 1 to 10, not 11.
list() turns a range into a list, so you can see all its numbers at once.
▶print(list(range(5)))2print(list(range(1, 11)))3print(list(range(1, 11, 2)))4for n in range(5, 21, 5):5 print("Count by fives:", n)
No variables yet.
▌✏️ Change this code and run it yourself
[0, 1, 2, 3, 4] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [1, 3, 5, 7, 9] Count by fives: 5 Count by fives: 10 Count by fives: 15 Count by fives: 20
💡 With one number, range() starts at 0. With two, the step is 1.
How do I count backwards with range()?
Use a negative step to count down. The start must now be bigger than the stop.
range(10, 0, -1) gives 10 down to 1. Again, the stop number 0 is left out.
Here is a countdown, like the ones before an ISRO rocket launch.
▶for sec in range(5, 0, -1):2 print(sec)3print("Lift off!")4print(list(range(10, 0, -3)))
No variables yet.
▌✏️ Change this code and run it yourself
5 4 3 2 1 Lift off! [10, 7, 4, 1]
What does enumerate() do in Python?
Often we want each item and its position. One way is to keep our own counter variable.
enumerate() does that counting for you. Each time round, it hands over a pair: the index and the item.
We catch the pair with two variables, i and fruit. Compare the two loops below.
▶fruits = ["mango", "guava", "jamun"]2count = 03for fruit in fruits:4 print(count, fruit)5 count += 16for i, fruit in enumerate(fruits):7 print(i, fruit)
No variables yet.
▌✏️ Change this code and run it yourself
0 mango 1 guava 2 jamun 0 mango 1 guava 2 jamun
💡 Both loops print the same thing. The enumerate() loop is shorter and cannot forget to add 1.
How do I start enumerate() counting from 1?
Index numbers start at 0, but people count from 1. A rank list should say 1st, not 0th.
Give enumerate() a second value, start=1. The counting then begins at 1.
This works with any list, and pairs nicely with f-strings from the last lesson.
Place 1: Riya Place 2: Kabir Place 3: Sana Place 4: Dev [(1, 'a'), (2, 'b')]
💡 The last line shows what enumerate() really makes: pairs called tuples, like (1, 'a').
When do I use range(len()) instead of enumerate()?
To change items inside a list, you need their index. range(len(prices)) gives every index.
enumerate() also gives the index, plus the item, so the code reads more clearly.
If you only need the items, use a plain for loop. Pick the simplest tool for the job.
[45, 30, 65] [90, 60, 130]
How do I make a numbered menu with enumerate()?
Apps often show a numbered menu and ask you to type a number.
enumerate(menu, start=1) prints the numbers. The user's choice is then one more than the index.
So we subtract 1 to find the right item: menu[choice - 1].
1. Masala dosa 2. Idli sambar 3. Poha Type a number: 2 You ordered Idli sambar.
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
print(list(range(2, 10, 3)))
Question 2. What does this print?
for i, c in enumerate("abc"):
print(i, c)Question 3. What does this print?
names = ["Asha", "Ravi"]
for n, name in enumerate(names, start=1):
print(n, name)🧩 Your challenge
Ask for your 3 favourite places to visit. Store them in a list. Print them as a numbered list starting at 1, using enumerate(). Then print them again in reverse order, using range() with a negative step.
Favourite place: Goa Favourite place: Jaipur Favourite place: Munnar My top places: 1. Goa 2. Jaipur 3. Munnar Reverse order: Munnar Jaipur Goa
⚠️ Common mistakes (and how to fix them)
❌ Wrong
for day in range(1, 7):
print("Day", day)No error message – but it prints Day 1 to Day 6, not Day 7.
✅ Right
for day in range(1, 8):
print("Day", day)range() stops just before the stop number. To include 7, the stop must be 8.
❌ Wrong
for sec in range(5, 0):
print(sec)
print("Lift off!")No error message – but the countdown prints nothing at all.
✅ Right
for sec in range(5, 0, -1):
print(sec)
print("Lift off!")Without a step, range() counts upwards. You cannot go up from 5 to 0, so the range is empty.
❌ Wrong
names = ["asha", "ravi"]
for name in enumerate(names):
print(name.upper())AttributeError: 'tuple' object has no attribute 'upper'
✅ Right
names = ["asha", "ravi"]
for i, name in enumerate(names):
print(i, name.upper())enumerate() gives pairs, like (0, 'asha'). Use two loop variables to split each pair.
📌 Remember
- range(start, stop, step) makes numbers from start up to, but not including, stop.
- A negative step counts backwards, so start must be bigger than stop.
- enumerate() gives each item together with its index number.
- enumerate(items, start=1) counts from 1, which suits ranks and menus.
- range(len(items)) or enumerate() let you change items by index.
- Use a plain for loop when you only need the items.
Finished the quiz and the challenge?
Questions students ask
Why does range() leave out the stop number?
It makes range(len(items)) give exactly the right indexes. A list of 5 items has indexes 0 to 4.
Is enumerate() better than using a counter variable?
Yes, usually. It is shorter, and you can never forget to add 1 to the counter.
Can enumerate() work on strings and dictionaries?
Yes. On a string it gives each letter. On a dictionary it gives each key with a number.
How do I reverse a list without range()?
You can loop over reversed(items), or use the slice items[::-1]. Counting down with range() is still good practice.