Python For Loops
A for
loop is used to execute a code block for a specified number of times.
When it comes to iterating through a sequence, a for
loop is used (that is either list, tuple, dictionary, set, or string).
Example :- Print each fruit from a list :
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Output :-
banana
cherry
The for
loop don't requires no previously defined indexing variable.
Looping Through a String
Strings, even if they are iterable objects, have a series of characters :
Example :- Loop through the letters in the word "banana" :
for x in "banana":
print(x)
Output :-
a
n
a
n
a
The break Statement
We can interrupt the loop with the break
declaration, before all elements are looped through :
Example 1 :- Exit the loop when x is "banana" :
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
Output :-
The continue Statement
We can halt the current loop iteration using the continue
statement and proceed with the next :
Example :- Do not print banana :
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
Output :-
cherry
Related Links
The range() Function
To loop a given number of times through a code set, the range()
method can be used.
The range()
method returns a number series, start with 0 and increasing by 1 by default and ending with a specified number.
The range()
function has three parameters, which is:
- Starting Number :- The default start value is "0". You can define from where the loop should start.
- Ending Number :- You can set an ending number for the loop.
- Increment :- How much to be increment after every loop iteration completion.
Example 1 :- Using the range()
function:
for x in range(6):
print(x)
Output :-
1
2
3
4
5
In the above program, we did not specified an start value and increment value. So it used the default start value as "0" and the default increment counter as "1".
Note:- It should be noted that range(6)
is not 0 to 6 then but 0 to 5.
Example 2 :- Using the start parameter :
for x in range(2, 6):
print(x)
Output :-
3
4
5
By default, the range()
feature increases the sequence by 1 but it can be specified by adding a third parameter : range(2, 30, 3)
:
Example 3 :- Increment the sequence with 3 (default is 1) :
for x in range(2, 30, 3):
print(x)
Output :-
5
8
11
14
17
20
23
26
29
Else in For Loop
The else
keyword in a loop provides a code block for running when the loop is complete :
Example 1 :- Display all of 0 to 5 numbers and then, when the loop ends, print a message :
for x in range(6):
print(x)
else:
print("Finally finished!")
Output :-
1
2
3
4
5
Finally finished!
Note : If the loop is halted by a break
statement, the else
block is NOT executed.
Example 2 :- Break the loop when x
is 3 and see the else
block :
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
Output :-
1
2
Note:- If the loop breaks, the else block is not executed.
Related Links
Nested Loops
There is a nested
loop within a loop.
For every iteration of the "outer loop" the "inner loop" is performed once :
Example :- For every fruit, print every adjective :-
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
Output :-
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
The pass Statement
The for
Loops cannot be empty, but if you have a loop with no content for
some reason, put the pass
statement in order to avoid error.
for x in [0, 1, 2]:
pass
# having an empty for loop like this, would raise an error without the pass statement
Note:- Having an empty for loop like this, would raise an error without the pass statement.