Skip to content

Python While loop

Python While Loop

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 value 4 .
  • Then we have written a while with condition number <7 . Whenever this condition becomes True the program will print the number and increment the number by 1 .
  • At first while will check if number <7(4<7) or not . As 4<7 is True so 4 is printed and number is incremented by 1 and become 5
  • Then as 5<7 is True so 5 is printed and number is incremented by 1 and become 6
  • Then as 6<7 is True so 6 is printed and number is incremented by 1 and become 7
  • Now 7<7 is False 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 variables name and number as "rajkumar" and 0
  • Then the program will check if number < len(name) (0<8) is True or not . As 0<8 is True so program will enter to the loop and print name[0] ("r") . After that number variable will be incremented by 1 and become 1 .
  • Then the program will check if number < len(name) (1<8) is True or not . As 1<8 is True so program will enter to the loop and print name[1] ("a") . After that number variable will be incremented by 1 and become 2 .
  • 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 with set datatype and printed all the elements of the set .
  • As we can not slice set so we need to convert it into a list using list function before printing . We can also see that elements of the set are not printed as the given order because set 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 with dictionary datatype and printed all the keys of the dictionary .
  • As we can not slice myDict.keys() so we need to convert it into a list using list 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 the for loop will be exited forcefully and the code in else block will not be executed .
  • But if program do not face break statement then for loop will be completed successfully and the code in else 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 .

Leave a Reply