Python For Loop

Definition

Python For Loop is used to iterate over a sequence of Python’s iterable objects like list, strings, tuple, and sets or a part of the program several times.

Syntax

for iterating_variable in python_sequence:
    # part of code 

Examples

Example1

# example for string
myStr = "I am a string"
for x in myStr:
    print(x)  # spaces will also print

Output

I
 
a
m

a

s
t
r
i
n
g

Example2

# example for list
myList = ["I", "am", "a", "list"]
for x in myList:
    print(x)

Output

I
am
a
list

Example3

# example for Tuple
myTuple = ("I", "am", "a", "Tuple")
for x in myTuple:
    print(x)

Output

I
am
a
Tuple

Example4

# example for set
mySet = {1, 2, 3, 4, 5}
for x in mySet:
    print(x)

Output

1
2
3
4
5

Example5

# example for dictionary
myDict = {"key1":"value1", "key2":"value2", "key3":"value3"}
for x in myDict:
    print(x,":",myDict[x])

Output

key1 : value1
key2 : value2
key3 : value3

range()

"range()" is an in-built function in Python which takes three integers as parameters and returns a sequence of numbers.

Syntax

range(start, stop, step)
ParameterDescription
startoptional, default is 0
stopRequired, topmost position(stop-1)
stepoptional, this is the increment in value, default is 1

Examples

Example1

for x in range(5):
    print(x) # starts with 0(default)
             # 5 not included (5-1=4)

Output

0
1
2
3

Example2

for x in range(1, 5):
    print(x) # starts with 1

Output

1
2
3
4

Example3

for x in range(2, 11, 3):
    print(x)  # starts with 2
              # ends on 11-1=10
              # 2+3=5
              # 5+3=8
              # 8+3=11(not included)

Output

2
5
8

Example4

myList = ["This", "is", "my", "list"]
for x in range(len(myList)):
    print(myList[x])

Output

This
is
my
list

Here "len(myList)" wil return 3 since the "range()" function will return a sequence upto 3 i.e. 0, 1, 2, 3 and myList[x] will print the lists’s element present at those index numbers.

break statement

"break" statement is used to terminate a loop. If the program executes the "break" then the loop will automatically get terminated whether the loop has iterated all the elements or not.

Example1

for x in range(5):
    print(x)
    if x == 3:
        break # nothing will print after 3

Output

0
1
2
3

Example2

myList = [1, 2, 3, 4, 5]
for x in myList:
    if x == 3:
        break
    print(x)

Output

1
2

continue statement

If the program reaches the continue statement then the program skips the current step of iteration of the loop and continues with the next step of the loop.

Example

myList = [1, 2, 3, 4, 5]
for x in myList:
    if x == 3:
        continue
    print(x)

Output

1
2
4
5

pass statement

Any loop or function in Python can not be empty as it will give an error but Python gives pass statement if you want an empty loop or function.

Example

for x in range(5):
    pass

for…else…

Python provides a feature where we can use else with for loop and while loop as well, while most of the programming does not have this feature.

The else block will get executed if the for/while loop is not terminated with a break statement.

Example1

# for loop without break
for x in range(5):
    print(x)
else:
    print("else block")

Output

0
1
2
3
4
else block

Example2

# for loop with break
for x in range(5):
    print(x)
    if x == 3:
        break
else:
    print("else block will not executed")

Output

0
1
2
3

Nested for loop

We can have loop inside a loop which is called nested loops.

For each step of outer loop, inner loop will execute whole i.e. all steps every time.

Example

for x in range(2):
    for y in range(2):
        print(x, y)

Output

0 0
0 1
1 0
1 1