For Loops

Repeating with For Loops

For loops let you repeat code for each item in a sequence (like a list or range of numbers). They're one of the most powerful tools in programming.
python
# Loop through a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(f"I like {fruit}")

Using range()

The range() function generates a sequence of numbers. It's commonly used when you want to repeat something a specific number of times.
python
# range(5) = 0, 1, 2, 3, 4
for i in range(5):
    print(f"Count: {i}")

# range(start, stop, step)
for i in range(0, 10, 2):
    print(f"Even: {i}")

Tip:range(n) generates numbers from 0 to n-1 (not including n). Use range(start, stop) or range(start, stop, step) for more control.

PlaygroundPython
Output
Click "Run" to see output

💬 Got questions? Ask me anything!