← Back to Python Course
πŸ”„ Loops & RepetitionΒ·20 minΒ·Beginner
πŸ”

For Loops

Doing things again and again

Learn how for loops let you repeat code without copy-pasting. Tell Python how many times to repeat and it just does it!

πŸ“–

What we are learning

A for loop tells Python to repeat a block of code a certain number of times. Instead of writing print("Hello") ten times, you write a loop that says "do this 10 times." It is cleaner, faster, and way less typing!

πŸ’‘ Think of it like this:

Imagine you have to hand out candy to 10 friends. Instead of saying "give candy" 10 times, you say "for each friend, give them candy." The loop handles the repetition for you!

🌟

Why it matters

Loops are one of the most powerful tools in programming. They let you process lists, repeat actions, draw patterns, and much more β€” all with just a few lines of code.

Where you see this in real life:

  • βœ“Games use loops to animate frames and move characters
  • βœ“Music apps use loops to play notes in sequence
  • βœ“Factories use loops (in robots) to assemble products
🎯

By the end, you can:

  • 1.Write a basic for loop
  • 2.Loop through a list of items
  • 3.Use the loop variable inside the loop
πŸ”€

New words

forloopiterationloop variable
🐍
If you had to say "Python is awesome" 5 times, how would you do it without a loop? Now imagine doing it 100 times!
πŸ€“

Fun Fact!

The word "loop" comes from the idea of a circle β€” you go around and around until you are done!

πŸ” For Loops

A forloop tells Python to repeat a block of code. Instead of writing the same line 10 times, you write it once and tell Python to repeat it!

✨ Your First For Loop

The simplest for loop uses range()to count. Each time through the loop, the variable changes to the next number.

🐍Try a for loop!
main.py
● Output
range(5) counts from 0 to 4 β€” that is 5 times!

πŸ’‘ The variable i changes each time: 0, 1, 2, 3, 4

πŸ“‹ Looping Through a List

You can also loop through items in a list. The loop variable becomes each item in turn!

🐍Loop through a list
main.py
● Output
Click β–Ά Run to see the output!

πŸ”’ Counting with Loops

Loops are great for counting! You can add up numbers, count down, or do math with the loop variable.

🐍Add up numbers 1 to 10
main.py
● Output
Click β–Ά Run to see the output!

🎨 Fun with Loops

Loops can create patterns! Try this triangle maker:

🐍Make a star triangle!
main.py
● Output
Click β–Ά Run to see the output!

πŸ’‘ Each time through the loop, i gets bigger, so more stars are printed!

🐍
Try this: use a for loop to print your name 5 times. Then try printing numbers from 1 to 20!
🧠

Quick Quiz!

+10 XP

What does range(5) produce in a for loop?

🐍
Finished the lesson? Click the button to mark it complete and earn XP!