Page Contents
Python while loop 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 .
Python While loop Syntax
while condition : statements
Python While loop Structure
- Python while loop starts with a
while
keyword followed by the condition and:
(colon) - Then we need to write a block of code with proper indentation .
Python While loop Examples
Example 1:
# while loop in python with numbers
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 infinite loop we need to click ctrl + c together in VS code . If we work on Jupyter notebook then we need to click on interrupt the kernel button .
Example: Infinite while loop in Python
# infinite while loop in python
number =4
while number <7:
print(number)
In the above code as we do not increment the number
so number <7
never becomes False
and loop while run forever .
Example 2: Iterate a string using Python while loop
# python while loop with string datatype
name ="rajkumar"
number=0
while number < len(name):
print(name[number])
number+=1
r a j k u m a r
- In the above code we have used Python
while
loop with string datatype . For this reason first we have declared 2 variablesname
andnumber
as"rajkumar"
and0
- Then the program will check if
number < len(name)
(0<8
) isTrue
or not . As0<8
isTrue
so program will enter to the loop and printname[0]
("r"
) . After thatnumber
variable will be incremented by1
and become1
. - Then the program will check if
number < len
(name
) (1<8
) is True or not . As1<8
isTrue
so program will enter to the loop and printname[1]
("a"
) . After thatnumber
variable will be incremented by1
and become2
. - And the process will go on until the string ends .
Example 3: Iterate a list using Python while loop
# python while loop with list datatype
fruits = ["apple","guava","mango","banana"]
number=0
while number < len(fruits):
print(fruits[number])
number+=1
apple guava mango banana
In the above code we have used Python while
loop with list
datatype and printed all the elements of the list
.
Note :
Python while
loop follows same behavior with tuple
as like list
.
Example 4: Iterate a set using Python while loop
# python while loop with set datatype
fruits = {"apple","guava","mango","banana"}
number=0
while number < len(fruits):
print(list(fruits)[number])
number+=1
banana apple mango guava
- In the above code we have used Python
while
loop withset
datatype and printed all the elements of theset
. - As we can not slice
set
so we need to convert it into alist
usinglist
function before printing . We can also see that elements of theset
are not printed as the given order becauseset
is unordered .
Example 5: Iterate a dictionary using Python while loop
# python while loop with dictionary datatype
myDict ={"a":1,"b":2,"c":3}
number=0
while number < len(myDict):
print(list(myDict.keys())[number])
number+=1
a b c
- In the above code we have used Python
while
loop withdictionary
datatype and printed all the keys of thedictionary
. - As we can not slice
myDict.keys()
so we need to convert it into alist
usinglist
function before printing .
While-Else loop in Python
We can combine else
keyword with Python while
loop . For this reason we need to use break statement .
Syntax of while else loop in Python:
while condition : statements break else : statements
- If program face
break
statement then thefor
loop will be exited forcefully and the code inelse
block will not be executed . - But if program do not face
break
statement thenfor
loop will be completed successfully and the code inelse
block will be executed .
Example 1:
# while-else loop
number =4
while number <7:
print(number)
number+=1
else:
print("all number is printed")
4 5 6 all number is printed
In the above code as while
loop completed normally printing the all numbers so statement in else
block is executed and printed all number is printed
.
Example 2:
# while-else loop
number =4
while number <7:
print(number)
number+=1
if number==5:
break
else:
print("all number is printed")
4
In the above code as while
loop exited forcefully after facing the break
statement so statement in else
block is not executed .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .