The for loop

A for loop repeats a block once for each item in a sequence — a list, string, range, dictionary, file, anything iterable:

for fruit in ["apple", "kiwi", "pear"]:
    print("I like", fruit)

for ch in "abc":
    print(ch.upper(), end=" ")
print()
I like apple I like kiwi I like pear A B C

Unlike C-style languages there is no counter to manage; Python hands you each item in turn.

range()

When you need numbers, range(start, stop, step) generates them lazily. stop is exclusive:

for i in range(5):        print(i, end=" ")   # 0..4
print()
for i in range(2, 7):     print(i, end=" ")   # 2..6
print()
for i in range(10, 0, -3): print(i, end=" ")  # 10 7 4 1
print()
print(list(range(0, 20, 5)))
0 1 2 3 4 2 3 4 5 6 10 7 4 1 [0, 5, 10, 15]

enumerate() and zip()

Need the index as well as the item? Use enumerate. Walking two lists side by side? Use zip:

names = ["Ada", "Linus", "Grace"]
langs = ["Analytical Engine", "Linux", "COBOL"]

for i, name in enumerate(names, start=1):
    print(f"{i}. {name}")

for name, lang in zip(names, langs):
    print(f"{name} -> {lang}")
1. Ada 2. Linus 3. Grace Ada -> Analytical Engine Linus -> Linux Grace -> COBOL

The while loop

while repeats as long as a condition stays true. Use it when you do not know in advance how many iterations you need:

import random
secret = random.randint(1, 10)
guess = None
tries = 0
while guess != secret:
    guess = int(input("Guess 1-10: "))
    tries += 1
print(f"Correct! It took {tries} tries.")
Guess 1-10: 5 Guess 1-10: 8 Correct! It took 2 tries.

Every while loop needs something inside it that eventually makes the condition false, otherwise it runs forever. If that happens on the phone, tap the stop button in the console.

break, continue and else

for n in range(2, 20):
    if n % 7 == 0:
        print("First multiple of 7:", n)
        break            # leave the loop entirely

for n in range(1, 8):
    if n % 2 == 0:
        continue         # skip to the next iteration
    print(n, end=" ")
print()

for n in [3, 5, 9]:
    if n % 2 == 0:
        print("Found an even number")
        break
else:
    print("No even numbers")   # runs only if loop was NOT broken
First multiple of 7: 7 1 3 5 7 No even numbers

The else on a loop trips people up: think of it as "no break happened".

Nested loops

for row in range(1, 4):
    for col in range(1, 4):
        print(f"{row * col:3}", end="")
    print()
1 2 3 2 4 6 3 6 9

Nested loops multiply work: two loops of 1,000 iterations run one million times. That is fine for learning, but keep it in mind when data gets big.

Run the loops in the app's built-in FizzBuzz example — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
Get it free

Exercise: FizzBuzz

Print the numbers 1 to 30, but print "Fizz" for multiples of 3, "Buzz" for multiples of 5 and "FizzBuzz" for both. This is the classic interview warm-up and one of the 30 built-in examples in the Coding Python app.

for n in range(1, 31):
    if n % 15 == 0:
        print("FizzBuzz")
    elif n % 3 == 0:
        print("Fizz")
    elif n % 5 == 0:
        print("Buzz")
    else:
        print(n)
1 2 Fizz 4 Buzz Fizz 7 ...

Loops and lists go together, so Python lists are next.

Frequently asked questions

What is the difference between a for loop and a while loop?

A for loop iterates over a known sequence of items; a while loop repeats until a condition becomes false. Use for when you know what you are looping over, while when you are waiting for something to happen.

How do I loop with an index in Python?

Use enumerate(): for i, item in enumerate(items). Avoid range(len(items)) unless you genuinely need only the index.

How do I stop an infinite loop?

Make sure the loop body changes the condition, or use break. In the Coding Python app, tap the stop button in the console to kill a runaway program.

What does the else clause on a loop do?

It runs once after the loop finishes normally, but is skipped if the loop ended via break. It is useful for 'searched everything and found nothing' logic.