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:
Output: complete Python program
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
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:
Output: complete Python program
for x in "banana":
print(x)
The break Statement
A break statement ends the loop early, before every item has been visited:
Example
Leave the loop once x is "banana":
Output: complete Python program
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Example
Leave the loop at "banana" again, but with the break placed ahead of the print:
Output: complete Python program
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
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:
Output: complete Python program
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
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():
Output: complete Python program
for x in range(6):
print(x)
Example
Choosing where the sequence starts:
Output: complete Python program
for x in range(2, 6):
print(x)
Example
Counting up in steps of 3 instead of the default 1:
Output: complete Python program
for x in range(2, 30, 3):
print(x)
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:
Output: complete Python program
for x in range(6):
print(x)
else:
print("Finally finished!")
Example
Here the loop breaks when x is 3, so watch what happens to the else block:
Output: complete Python program
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
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:
Output: complete Python program
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
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
Output: complete Python program
for x in [0, 1, 2]:
pass