Python For Loops

If you want to execute some action for every element present in some sequence(it may be string or collection) then you should go for for loop.

Basic syntax:

for x in sequence:
    statements

Where sequence can be string or any collection. Statements will be executed for every element present in the sequence.

You can print characters present in the given string as follows,

s="Python"
for x in s :
print(x)

Output

P
y
t
o
n

Pass Statement

You cannot empty for loops but if for some reason have a for loop without content then put a pass statement in the loop to avoid getting any error.

for x in [012]:
    pass

Range() Function

To iterate through a set of code a specified number of times you can use the range() function, The range() function returns a sequence of numbers starting from 0 (zero) by default and increments by 1 but ends at a specified number.

Using the range() function:

for x in range(6):
    print(x)

Output

0
1
2
3
4
5 

Note: range(6) is not the values of 0 to 6 but the values 0 to 5.

The range() function by default starting value from 0 (zero) however it is possible to specify the starting value by adding a parameter range(2, 6) which means values from 2 to 6 (but excluding 6):

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

Output

2
3
4
5

The range() function by defaults increment the sequence by 1  however it is possible to specify the increment value by adding a third parameter range(2, 30, 3) Increment the sequence with 3.

for x in range(2303):
    print(x)

Output

2
5
8
11
14
17
20
23
26
29

Else function in For Loop

Else keyword in a for loop is required to specifies a block of code to be executed when the loop is finished.

Print all numbers from 0 to 5 and print a message when the loop got ended,

for x in range(6):
    print(x)
else:
    print("loop finished!")

Output

0
1
2
3
4
5
loop finished!

Break Statement

Using break statement you can stop the loop before it has looped through all the items.

Exit the loop when x is 2,

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

Output

1
2

Continue Statement

Using continue statement you can stop the current iteration of the loop and continue with the next.

Here do not print 3,

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

Output

1
2
4
5

Nested Loops

A nested loop is nothing but a loop inside another loop. The inner loop executes one time for each iteration of the outer loop. Here print each adjective for every fruit,

adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
    for y in fruits:
        print(x, y)

Output

red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana

Read Also : Differences between List and Tuple in Python

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

Leave a Comment