Page Contents
Loops in Python helps us to repeat a specific block of code . In Python there are two types of loop one is While loop and other is For loop . Although basic functionalities of both the loops are same but syntax is different .
While loop in Python
While loop in Python is used to execute a block of code repeatedly as long as the given condition True
and when the condition becomes False
the program exits the loop .
Syntax :
while condition : statements
Example 1:
# while loop in python
number =4
while number <7:
print(number)
number+=1
4 5 6
- In the above code first we have declared a variable
number
with value4
. - Then we have written a
while
with conditionnumber <7
. Whenever this condition becomesTrue
the program will print thenumber
and increment thenumber
by1
. - At first
while
will check ifnumber <7
(4<7
) or not . As4<7
isTrue
so4
is printed andnumber
is incremented by1
and become5
- Then as
5<7
isTrue
so5
is printed andnumber
is incremented by1
and become6
- Then as
6<7
isTrue
so6
is printed andnumber
is incremented by1
and become7
- Now
7<7
isFalse
so program exited the loop .
Note :
In the above code we should always increment the number
otherwise condition in while
loop never becomes False
and the loop will be an infinite loop .
To break the indefinite loop we need to click ctrl + c together .
For loop in Python
For loop in Python is used to iterate over a iterator ( string , list , tuple , set , dictionary) or iterable objects .
Syntax :
for value in iterator : statements
Example 1:
# for loop in python
for name in ["Rajkumar" , "Sourav" , "Sudipta"]:
print(name)
Rajkumar Sourav Sudipta
- In the above code for-loop will iterate every element in the
list
- At first ,
for-loop
will go to the first element of thelist
that is"Rajkumar"
and declare thename
variable as"Rajkumar"
and print thename
variable . - Then ,
for-loop
will go to the second element of thelist
that is"Sourav"
and redeclare thename
variable as"Sourav"
and print thename
variable . - After that ,
for-loop
will go to the third element of thelist
that is"Sudipta"
and redeclare the name variable as"Sudipta"
and print thename
variable . - when the
list
will end the program will exit from thefor-loop
.
Break statement in Python
Break statement in Python can be used to exit from the innermost loop without looping through all the items .
# break statement with while loop
number =4
while number <7:
number+=1
if number==6:
break
print(number)
5
- In the above code we have implemented
break
statement withwhile
loop - At first , as
number
is4
so it will enter in thewhile
loop . Then thenumber
will be incremented by1
and become5
. Then it will check if thenumber==6
(5==6
)True
or not . As5==6
isFalse
so it will not enter in theif
block andprint
the number - Now
number
is5
so it will enter in thewhile
loop .Then thenumber
will be incremented by1
and become6
. Then it will check if thenumber==6
(6==6
)True
or not . As6==6
isTrue
so it will enter in theif
block and face thebreak
statement . After facing thebreak
statement program will exit immediately fromwhile
loop without completing the other iterations . .
Continue statement in Python
Continue statement in Python can be used to escape the current iteration and continue with the next iteration .
Example 1:
# continue statement with while loop
number =4
while number <7:
number+=1
if number==6:
continue
print(number)
5 7
- In the above code we have implemented
continue
statement withwhile
loop - At first , as
number
is4
so it will enter in thewhile
loop . Then thenumber
will be incremented by1
and become5
. Then it will check if thenumber==6
(5==6
)True
or not . As5==6
isFalse
so it will not enter in theif
block and print thenumber
. - Now
number
is5
so it will enter in thewhile
loop .Then thenumber
will be incremented by1
and become6
. Then it will check if thenumber==6
(6==6
)True
or not . As6==6
isTrue
so it will enter in theif
block and face thecontinue
statement . After facing thecontinue
statement program will escape the current iteration and continue with the next iteration . - Now
number
is6
so it will enter in thewhile
loop . Then thenumber
will be incremented by1
and become7
. Then it will check if thenumber==6
(7==6
)True
or not . As7==6
isFalse
so it will not enter in theif
block and print thenumber
- Now
number
is7
so it will not enter in thewhile
loop .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .