TalentJR

🐍 Python for kids · Class 6 · Lesson 4 of 18 · Free

For Loops – Go Through Every Item

⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in

In this lesson you will

  • Loop over numbers with range(start, stop, step), including counting backwards.
  • Loop over the letters of a string and the items of a list.
  • Count, add up and find the biggest value with a loop and a helper variable.

Your class teacher has 40 answer sheets to check. She picks up each sheet, checks it, and moves to the next one. A for loop does exactly this: it visits every item in a group, one by one.

How does a for loop work with range()?

A for loop takes the next value from a sequence and runs its block. It repeats until the values run out.

range(start, stop, step) makes the numbers. The stop value is never included.

A negative step counts backwards. This is how you write a countdown.

🎬 How Python thinks — step 1 of 11Python is about to run line 1
▶for t in range(10, 0, -3):
2 print("T minus", t)
3print("Lift off! Chandrayaan is on its way.")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
T minus 10
T minus 7
T minus 4
T minus 1
Lift off! Chandrayaan is on its way.

💡 range(10, 0, -3) gives 10, 7, 4, 1. The next would be -2, which is past the stop value 0.

How do I loop through a string in Python?

A string is a row of characters. A for loop can visit each character in turn.

Combine it with an if to count special letters. Here we count the vowels in a word.

The counter starts at 0 and goes up by 1 only when the letter is a vowel.

🎬 How Python thinks — step 1 of 27Python is about to run line 1
▶word = "Bengaluru"
2vowels = 0
3for letter in word:
4 if letter.lower() in "aeiou":
5 vowels = vowels + 1
6print(word, "has", vowels, "vowels")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Bengaluru has 4 vowels

How do I loop through a list and add up the items?

A for loop on a list gives you each item directly. You do not need index numbers.

To add things up, keep a total that starts at 0. Each round adds the current item.

After the loop, divide by len() to get the average.

🐍 Python — you can edit this code
Output
Total: 393
Average: 78.6

How do I find the biggest number with a for loop?

Python has max(), but building it yourself teaches you how it works.

Start by guessing that the first item is the biggest. Then compare each item with your best so far.

If an item is bigger, it becomes the new best. At the end, best holds the winner.

🎬 How Python thinks — step 1 of 17Python is about to run line 1
▶scores = [45, 82, 17, 99, 64]
2best = scores[0]
3for s in scores:
4 if s > best:
5 best = s
6print("Highest score:", best)
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Highest score: 99

How do I get the position and the item together?

Sometimes you need the position number too, like for a rank list. enumerate() gives both.

Each round, it hands you a pair: a count and an item. Write two variable names to catch them.

start=1 makes the counting begin at 1, which suits ranks better than 0.

🐍 Python — you can edit this code
Output
Rank 1: Diya
Rank 2: Rohan
Rank 3: Ayesha

How do I skip or stop inside a for loop?

continue skips the rest of the current round. break leaves the loop at once.

Below, the loop skips absent students, shown as -1. It stops completely at the first fail mark.

🐍 Python — you can edit this code
Output
Checked: 72
Absent, skipping
Checked: 88
Found a fail mark: 25
Done checking.

💡 90 is never checked, because break ended the loop at 25.

🤔 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?

for i in range(2, 12, 4):
    print(i)

Question 2. What does this print?

count = 0
for ch in "banana":
    if ch == "a":
        count = count + 1
print(count)

Question 3. What does this print?

total = 0
for n in [5, 10, 15, 20]:
    if n == 15:
        break
    total = total + n
print(total)

🧩 Your challenge

Weather report! Here are 7 days of temperatures. Use one for loop to find the total, the hottest day's temperature, and how many days were above 35. Print all three answers.

Expected output :
Total: 246
Hottest: 39
Days above 35: 4
🐍 Python — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

marks = [80, 90, 70]
for m in marks:
    total = 0
    total = total + m
print(total)

No error message – but it prints 70, not the real total 240.

✅ Right

marks = [80, 90, 70]
total = 0
for m in marks:
    total = total + m
print(total)

total = 0 inside the loop resets the total every round. Set the starting value once, before the loop.

❌ Wrong

for i in range(5, 0):
    print(i)
print("Countdown done")

No error message – but no numbers print at all.

✅ Right

for i in range(5, 0, -1):
    print(i)
print("Countdown done")

range counts up by default. To count down, give a negative step like -1.

❌ Wrong

for n in range(3):
print(n)

IndentationError: expected an indented block after 'for' statement on line 1

✅ Right

for n in range(3):
    print(n)

The lines that repeat must be indented under the for line. Without spaces, the loop has no body.

📌 Remember

  • A for loop visits every value in a range, string or list, one at a time.
  • range(start, stop, step) stops before stop; a negative step counts down.
  • A counter or total starts before the loop and changes inside it.
  • To find the biggest item, keep a best-so-far and compare each item with it.
  • enumerate() gives the position and the item together.
  • continue skips one round; break ends the loop.

Finished the quiz and the challenge?

Questions students ask

What is the difference between for and while loops?

A for loop goes through a known group of values, like a list or a range. A while loop repeats until a condition becomes False.

Can a for loop go backwards in Python?

Yes. Use a negative step, like range(10, 0, -1), which counts 10 down to 1.

Can I change the loop variable inside a for loop?

You can, but it will not change the next value. The for loop always takes the next value from its sequence.

What does enumerate() do in Python?

It gives you a count along with each item. It saves you from keeping a separate counter variable.