Skip to content

Python while loop, for loop




Python – Loop


As we read earlier about Python Indexing and SlicingNow we will study about Loops in python programming. Python Loops are considered to be very important in all programming languages. Likewise in Python, it has a lot of important roles. We will covered python loop, types of loop, clause, some statements etc. so lets start





Loop in python

In python, there are the two types of loops are :

  • while loop

  • for loop



Guidelines before creating any loop in python :

  • Thinking about the initialization of the loop variable ( Start ).

  • Condition Checking ( End ).  

  • Increment and Decrement ( Step ).




While loop in python

A while loop will cause the loop statements to be executed until the condition is false.



while loop syntax python :

while (condition): 
    statement---
    ------------



Flowchart of while loop in python :

python-while-loop



Example :

i=0 
while (i<10): 
    print(i) 
    i+=1



Output :

0 
1 
2 
3 
4 
5 
6 
7 
8 
9




While loop can also run without a condition by using numbers (complex or real) or True.

If the condition is always true the while loop will run forever (i.e. Infinite loop) if it is not terminated by a break or return statement or an exception.



Example :

while True: 
    print("Infinite Loop")



Output :

Infinite Loop 
Infinite Loop 
Infinite Loop 
Infinite Loop 
Infinite Loop 
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ and so on.....





The Break statement in python

With the Break statement, we can stop the loop even if the while condition is true.



Example :

i=0
while (i<10):
    print(i)
    if i==5:
        break
    i+=1



NOTE : Exit the loop when i=5



Output :

0 
1 
2 
3 
4 
5




The Continue statement in python

With the continue statement, we can stop the current iteration and continue with the next.



Example :

i=0
while (i<10):
    i+=1
    if i==5:
        continue
    print(i)



NOTE : Continue to the next iteration if i is 5



Output :

1 
2 
3 
4 
6 
7 
8 
9 
10




The else statement in python

With the else statement, we can run a block of code once when the condition no longer is true.



Example :

i=0
while (i<10):
    print(i)
    i+=1
else:
    print("i is no longer less than 10")



NOTE : Print a message once the condition is false.



Output :

0 
1 
2 
3 
4 
5 
6 
7 
8 
9 
i is no longer less than 10






For loop in python

With the for loop, we can execute a set of statements ( i.e. List, Tuple, Dictionary, set etc ).



Flowchart of for loop in python :

python-for-loop




for loop using elements / sequence :



Example :

colours = ['Black', 'White', 'Red', 'Blue']
for i in colours:
    print(i)



Output :

Black 
White 
Red 
Blue




The Break statement in python

With the break statement, we can stop the loop before it has looped through all the items.



Example :

colours = ['Black', 'White', 'Red', 'Blue']
for i in colours:
    print(i)
    if i=="White":
        break



Output :

Black 
White




The continue statement in python

With the continue statement, we can stop the current iteration of the loop, and continue with the next.



Example :

colours = ['Black', 'White', 'Red', 'Blue']
for i in colours:
    if i=="White":
        continue
     print(i)



Output :

Black 
Red 
Blue




for loop using Range :

Range( ) is a built-in-function of python. It is used when a user needs to perform an action for a specific number of times. Range( ) function is also used to generate sequence of numbers.

Range( ) function are mainly three arguments :

  •  Start : Integer starting from which the sequence of integers is to be returned.

  •  Stop : Integer before which the sequence of integers is to be returned.

  •  Step : Integer value which determines the increment between each integer in the sequence.



Example :

for i in range(8): 
    print(i)



Output :

0 
1 
2 
3 
4 
5 
6 
7



The Range( ) function always starts from 0.
Note : The range(8) is not the values of 0 to 8 but the values 0 to 7.





The Range( ) function defaults to 0 as a string value
Range(2,6) which means values from 2 to 6 ( but not including 6 ).



Example :

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



Output :

2 
3 
4 
5




The Range( ) function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter.



Example : Range( 2,30,3 )

for i in range(2,30,3): 
    print(i)



Output :

2 
5 
8 
11 
14 
17 
20 
23 
26 
29




else clause in for loop

The else clause in for loop specifies a block of code to be executed when the loop is finished.



Example :

for i in range(8): 
    print(i) 
else: 
    print("Finished !")



Output :

0 
1 
2 
3 
4 
5 
6 
7 
Finished !




Nested loop in python

A Nested loop is a loop that occurs within another loop.

The inner loop will be executed one time for each iteration of the outer loop.



Example :

colours = ["Black", "White", "Red", "Blue"] 
cars = ["Mercedes", "BMW", "Audi", "Volvo"] 
for i in colours: 
    for j in cars: 
        print(i,j)



Output :

Black Mercedes 
Black BMW 
Black Audi 
Black Volvo 
White Mercedes 
White BMW 
White Audi 
White Volvo 
Red Mercedes 
Red BMW 
Red Audi 
Red Volvo 
Blue Mercedes 
Blue BMW 
Blue Audi 
Blue Volvo




The pass statement in python

For loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass statement to avoid getting an error.



Example :

for i in [0,1,2,3,4]: 
    pass



Output :


The above output will be blanked.


>