Skip to main content

Python For Loops

Use a for loop to go through a sequence, which can be a list, a tuple, a dictionary, a set or a string.

In many languages for works quite differently. In Python it behaves more like the iterator methods found in other object-oriented languages.

The statements in the loop run one time for every item in the list, tuple, set and so on.

You also don't need to set up an indexing variable in advance.

Example

Print every fruit in a list:

Prompt
Write a Python program that uses a for loop to print each fruit in a list. Create a list with "apple", "banana", "cherry" and loop through it.
Output: complete Python program

Looping Through a String

A string is a sequence of characters, so you can iterate over it as well:

Example

Go through the word "banana" letter by letter:

Prompt
Write a Python program that uses a for loop to iterate through each character in the word "banana". Print each letter on a separate line.
Output: complete Python program

The break Statement

A break statement ends the loop early, before every item has been visited:

Example

Leave the loop once x is "banana":

Prompt
Write a Python program that uses a for loop with break to exit when x is "banana". Print each fruit, then break after printing "banana".
Output: complete Python program

Example

Leave the loop at "banana" again, but with the break placed ahead of the print:

Prompt
Write a Python program that uses a for loop with break before print. Break when x is "banana", so "banana" is not printed.
Output: complete Python program

The continue Statement

A continue statement drops the current iteration of the loop and goes straight to the next one:

Example

Skip banana when printing:

Prompt
Write a Python program that uses a for loop with continue to skip "banana". Print all fruits except "banana".
Output: complete Python program

The range() Function

When you want to repeat some code a set number of times, range() is the tool to use.

It gives you a sequence of numbers. Unless you say otherwise, the sequence begins at 0, goes up by 1 each step, and stops at the number you specify.

That stopping number is not included, so range(6) covers 0 to 5 rather than 0 to 6.

To begin somewhere other than 0, pass the start as an extra parameter: range(2, 6) gives the values from 2 up to, but not including, 6.

A third parameter changes the step from its default of 1, as in range(2, 30, 3).

Example

A basic call to range():

Prompt
Write a Python program that uses a for loop with range(6) to print numbers 0 through 5.
Output: complete Python program

Example

Choosing where the sequence starts:

Prompt
Write a Python program that uses range(2, 6) to print numbers starting from 2 up to 5.
Output: complete Python program

Example

Counting up in steps of 3 instead of the default 1:

Prompt
Write a Python program that uses range(2, 30, 3) to print numbers from 2 to 29 incrementing by 3.
Output: complete Python program

Else in For Loop

Add the else keyword to a for loop to give it a block that runs once the loop has finished.

Note: If a break statement ends the loop, the else block does NOT run.

Example

Count from 0 to 5, then print a message at the end:

Prompt
Write a Python program with a for loop using range(6) and an else block. Print numbers 0-5, then print "Finally finished!" when the loop ends.
Output: complete Python program

Example

Here the loop breaks when x is 3, so watch what happens to the else block:

Prompt
Write a Python program demonstrating that else does not execute when break is used. Break when x equals 3 in a range(6) loop with an else block.
Output: complete Python program

Nested Loops

Placing one loop inside another gives you a nested loop.

For every single iteration of the "outer loop", the "inner loop" runs from start to finish:

Example

Pair every adjective with every fruit:

Prompt
Write a Python program with nested for loops. Loop through adjectives ["red", "big", "tasty"] and fruits ["apple", "banana", "cherry"], printing each combination.
Output: complete Python program

The pass Statement

A for loop needs a body. If yours has nothing to do for some reason, write pass there so that Python doesn't raise an error.

Example

Prompt
Write a Python program with a for loop that uses the pass statement as a placeholder. Loop through [0, 1, 2] with pass in the body.
Output: complete Python program