Python while loop generally used to perform the same action as for loop does, but in different way. Here is the general form to use while loop in python:
while expression: statement(s)
Here is an example demonstrates while loop in python:
# Python while Loop - Example Program i=0 while(i<10): print(i) i=i+1
Here is the sample output of the above python program:
Let's take another example program also demonstrates the use of while loop in python. This program perform the same action or the output of this program is same as the output of the program (of previous tutorial of for loop). The difference is, that program build up with for loop whereas this one with while loop:
# Python while Loop - Example Program while True: print("Enter 'x' for exit.") n = input("Enter any number: ") if n == 'x': break else: val = int(n) nr = int(input("Upto how many rows: ")) i = 0 j = 0 while i<nr: while j<i+1: print(val,end=" ") j = j + 1 j = 0 i = i + 1 print()
Here are sample runs of the above python program of while loop. This is the first snapshot:
This is the second screenshot:
Let's take an example on while loop. This example ask a question and continue asking until the user give the right or correct answer. Let's see how this example works.
# Python while Loop Example print("Welcome to while loop of Python."); print("What are you doing here ?"); what = input(); while(what != "learning python"): what = input("What ?\n"); print("Right..!!");
Here are some sample runs of the above while loop example in python.
This is first output:
Now type your answer and press enter key to see the output. This is the second output after entering wrong answer:
This is the third output again after entering the wrong answer:
This is the fourth output, now this time after entering the correct answer:
Here is another example of while loop on compound conditions with password protection feature in Python.
print("\tPassword protected system of CodesCracker"); print("\t\tMembers are allowed only\n"); username = ""; while not username: username = str(input("Enter username:")); pswrd = ""; while not pswrd: pswrd = str(input("Enter password:")); if username == "pythonProgrammer" and pswrd == "private": print("Welcome pythonProgrammer"); elif username == "pythonCoder" and pswrd == "alpha408": print("Welcome pythonCoder"); elif username == "deepak" and pswrd == "lovecoding": print("Welcome Deepak"); elif username == "devraj" and pswrd == "lovetouring": print("Welcome Devraj"); elif username == "dev" and pswrd == "lovesoftwaredeveloping": print("Welcome Dev"); elif username == "guest" and pswrd == "guest": print("Welcome guest"); else: print("Login failed!");
In this example, only members are allowed with following username and password listed in the following table:
If you will enter the username and password out of the above list, then Login failed! is the message you will see on the output screen. And if provide any of the above detail to login, then you will see the welcome message as shown in the following sample outputs.
If you press enter without typing any username, then the program again ask to enter the username and continue asking until you enter something for username as shown in the below screenshot:
Here is the output when you type username (of any member), devraj here, and wrong password, then you will see the following output:
Now, let's provide correct username and password to check out the output as shown in the following screenshot:
Here are list of some more examples, uses while loop in python: