Python While Loop

While loop is an statement which keeps on executing until the condition, based on which block is executing is true.

Syntax of while loop in python

while condition:
statement(s)

The loop iterates until the condition is true.

In While chart flow below.
1) While check for condition first, if it is true.
2) Go to the next statement and execute it.
3) if condition is false at the first place. It will skip the loop body and first statement after loop body will be executed
.

Example

count = 1
while count < 10:
       print( "The count is, ", count)
       count = count + 1
print("Out of the Loop now")

Output below in jupyter notebook
1) Watch out for the indentation
2) count = count + 1 is counter # count is just below the print statement – Take care

Infinite Loop
A loop is an infinite loop, if condition in the loop do not becomes false.
This is useful in programs where Client and server continuously interact with each other.

while code in one line with infinite loop
Can be written in one line, if it just has one statement after the while statement

count = 1
while count < 10: print( "The count is, ", count)

Output
This will go in infinite loop. # Press CTRL+C to exit the loop

Little Complex Code using while and if together
Dear learners, its time to do little complex coding. The program below is about Marc who is a dreamer to have the car license, since he was just 10 years old.
Seeing his overexcitement, his father who is a python expert created a program to make him understand the criteria of getting a license.
All, this is just the hypothetical conditions to apply for the License .
I would suggest you to check your local Driver License offices for the validity ! .

Lets understand what is the condition here
1) Marc is 10 year old.
2) Minimum age to get the license in USA is 16 years.
3) Except in the state of KY, which allows Marc to get the license one year earlier at age 15.
4) We are not sure where Marc lives and also till he turns 15, He is not valid to apply.
5) When he turns 15.
Program asks do you live in KY?
if Marc answers YES.
He would be eligible to have license in the state of ‘KY’
if answer is No
The message displays “Wait till you turn 16 to apply in other states”

Program

Age_toget_License = 16
Age_Marc = 10 
while(Age_Marc < Age_toget_License):
  # Loop Starts
    if(Age_Marc < 15 ):
               # condition
        print(Age_Marc," is not valid to have license")
 #Not Valid
        Age_Marc = Age_Marc + 1
    if(Age_Marc == 15 ):
        State = input("Do you live in KY ")
        if(State == 'ky'):
            print(Age_Marc," is valid to have license in KY")
        else:
            print(" Wait till you turn 16 to apply in other states")
    Age_Marc = Age_Marc + 1    #Condition to get out of loop

Output
if you enter ky

1 thought on “Python While Loop”

  1. Author has put in lot of efforts in creating the detailed context. Getting lot of insight to python now and at one place. Good place to learn & hands on python.

    Reply

Leave a Comment