Skip to main content
🐍 Python Basics 🐍

What's a Loop? πŸ”

Sometimes you want to do the same thing many times. Instead of copying a line over and over, Python uses a loop.

A for loop walks through a list of numbers. range(1, 4) gives you 1, 2, 3 (it stops before the last number):

total = 0
for i in range(1, 4):
    total = total + i   # adds 1, then 2, then 3
# total is now 6

Each time around the loop, i becomes the next number, and we add it to total. Read it, then answer below β€” you'll write one next. πŸ‘‡

What numbers does range(1, 4) give you?

After the loop above, what is total?

A loop is handy when you want to: