Python While Loop

The Python While Loop is used to repeat a block of statements for given number of times, until the given condition is False. A While loop in Python start with the condition, if the condition is True then statements inside the while loop will be executed. If the given condition is false then it won’t be executed at least once. It means, Python while loop may execute zero or more time and the syntax of while loop is:

Python While Loop Syntax

The Python While Loop Syntax is as follows:

While (Condition or Expression):
   statement 1
   statement 2
    ………….
# This is the statement Outside the While Loop but inside the Program

Once you hit the enter after semicolon, python while loop will start the next statement with Tab space and this Tab space acts as curly braces ({ }) in other programming languages.

First compiler will check the condition inside the Python While loop. If the Condition is True then the statement or group of statements under the while loop block will be executed. If the Condition is False then compiler will come out of the loop and execute other statements outside the Python while loop.

Python While Loop Flow Chart

This flow chart will explain you Python While loop Visually and perfectly

PYTHON WHILE LOOP FLOW CHART

Python While loop will check for the condition at the beginning of the loop.

  1. If the condition is True then it will execute the code inside the loop.
  2. Next we have to use Arithmetic Operator inside the Python while loop to increment and decrements the value.
  3. After the value incremented it will again check the condition. As long as the condition is True, the statements within the while loop will be executed.
  4. If the condition is False then it will exit from the While loop

Python While loop Example

Let us see the python while loop example for better understanding. This program for while loop allows the user to enter an integer value below 10. Using this value, compiler will add those values to sum up to 10.

# Example for While Loop

total = 0
number = int(input(" Please Enter any integer below 10:  "))
while (number <= 10):
    total = total + number
    number = number + 1
print(" Value of Total From the While Loop is: ", total)

We are going to enter number as 5. It means, total = 5 + 6 + 7 + 8 + 9 + 10 = 45

Please Enter any integer below 10: 5
Value of Total From the While Loop is: 45

First, we created a variable called total and it is initialized to 0. Following Python statement stores the user given values in variable number

number = int(input(" Please Enter any integer below 10:  "))

In the next line we used while condition. If the condition result is true then number is added to the total, otherwise it will exit from the loop. We also used + operator to increment the number value (number = number +1). After increment, the process will be repeated until the condition results False.

while (number <= 10):
    total = total + number
    number = number + 1

The print statement outside the while loop and it will be executed when the while condition is either True or False

print(" Value of Total From the While Loop is: ", total)

Python While loop Else Example

The Python programming allows us to use the else statement with While loop statements as well and it works like Python If Else statement.

  • If the while condition is True then statements inside the While Loop will be executed
  • If the While condition is False then statements inside the Else block will be executed. If we used Break statement to break the while loop then Else block will not executed. Please refer Python If Else and Break statement.

This While loop else program allows the user to provide an integer below 10. Using this value, compiler will add those values up to 10.

# Example for While Loop with Else Statement

total = 0
number = int(input(" Please Enter any integer below 10:  "))
while (number <= 10):
    total = total + number
    number = number + 1
    print(" Value of Total From the While Loop is: ", total)
else:
    print(" You Value is Greater Than 10 ==> This is from Else Block ")
Python While Loop with else

In the while condition.

while (number <= 10):

If the condition result is true then number is added to the total.

total = total + number

Next, We used + to increment number value. After increment, the process will be repeated until the condition returns False.

 number = number + 1

In the next line we used print statement inside the while loop and this statement will be display the value inside the total for every iteration

print(" Value of Total From the While Loop is: ", total)

If the while condition is False then following print statement will be executed

print(" You Value is Greater Than 10 ==> This is from Else Block ")

Here we entered value as 5, when it reached 10 while condition will fail so it will enter else block and print the statements inside the Else statement.

Infinite While Loop in Python Programming

If you forgot to increment or decrement the value inside the while loop then while loop will execute infinite times (also called as infinite loop).

# Infinite While Loop Example

x = 1
while x < 10:
    print("Value From the While Loop is: ", x)

Python Infinite While loop output

Value From the While Loop is: 1
Value From the While Loop is: 1
Value From the While Loop is: 1
Value From the While Loop is: 1
Value From the While Loop is: 1
Value From the While Loop is: 1
Value From the While Loop is: 1
Value From the While Loop is: 1
Value From the While Loop is:

Here x is always 1 and x is always less than 10 so while loop statement will go on execute infinite times. Now, let us add Arithmetic operator (x = x +1) inside the Python while loop to the above example.

# Infinite While Loop Example - Solution

x = 1
while x < 10:
    print("Value From the While Loop is: ", x)
    x = x + 1 # To increment X value

When it reaches 10 the while loop condition will fail.

Value From the While Loop is: 1
Value From the While Loop is: 2
Value From the While Loop is: 3
Value From the While Loop is: 4
Value From the While Loop is: 5
Value From the While Loop is: 6
Value From the While Loop is: 7
Value From the While Loop is: 8
Value From the While Loop is: 9