For Loops - The Perfect Counter

7-9 minutes Beginner Level

Understanding the Concept

For loops are awesome when you know exactly how many times you want to do something, or when you want to go through a list of items one by one. It's like having a super reliable assistant who goes through your to-do list item by item. For loops are cleaner and more predictable than while loops for many tasks!

Code Example

# Count with range()
for i in range(5):
    print("Number:", i)

# Loop through a list of items
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
    print("I love", fruit)

# Count from 1 to 5 (instead of 0 to 4)
for i in range(1, 6):
    print("Count:", i)
💡 Explanation: Use range() to count numbers, or loop directly through lists. The variable (i, fruit) gets each value automatically!
Quick Actions

Your Turn to Practice

Create a for loop that prints the squares of numbers from 1 to 5 (1, 4, 9, 16, 25). Hint: use number * number!

Code Sandbox
Ready to run your code! 🚀
Pro tip: Write your solution in the sandbox above and click "Run Code" to test it! Ask our Python tutor if you need help!

Knowledge Check

Key Topics
for loop range iterate list count sequence repeat
Learning Path
Getting Started
Variables
Data Types
Python Syntax
Comments
Numbers
Operators
Type Casting
Strings
Booleans
User Input
If...Else
While Loops
For Loops
Lists
Dictionaries
Functions