Welcome back friends to another post on TheCodingProject and we are back with the sixth chapter of the Beginner Python programming tutorial - How to become a python Jedi. This time we are going to learn how to use while loop in python.
The While Loop statement?
In Python Programming, There are 3 types of loops →
- While loop
- For loop
- Nested loops
Now to get into some action→
Let’s consider that two groups of aliens have landed on earth out of these two one of the group is good & the other are the bad guys. The govt. Has given you the task of designing a system that takes in input from different eyewitnesses & then classify that whether the aliens that these eyewitnesses encounter are the good guys or bad guys.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aliens = input() # ask for user input | |
while aliens is not "Autobots": | |
print( | |
"Who is your leader : ") # ask for user input | |
leader = input() | |
if leader == "Megatron": # if resolves to True | |
print( | |
"You are one of the bad guys") # print this statement and exit | |
break |
Copy & paste the above code in your editor and run this code. What do you get ? When the input is Autobots it matches the condition aliens == "Autobots" & it enters the loop to prints the statement pointed by the print method.
We can also use negative conditions for entering the while loop. Copy and paste the below code in your editor and run it→
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aliens = input() # ask for user input | |
while aliens is not "Autobots": | |
print( | |
"Who is your leader : ") # ask for user input | |
leader = input() | |
if leader == "Megatron": # if resolves to True | |
print( | |
"You are one of the bad guys") # print this statement and exit | |
break |
As you can see the condition aliens is not "Autobots" resolves to False when the user inputs a wrong condition & the control enters the loop and print the statement when a false condition is returned.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aliens = input() # ask for user input | |
while aliens is not "Autobots": | |
print( | |
"Who is your leader : ") # ask for user input | |
leader = input() | |
if leader == "Megatron": # if resolves to True | |
print( | |
"You are one of the bad guys") # print this statement and exit | |
break |
So, folks this was all for this week but we shall return with another chapter of this tutorial with some new concepts to learn.
Take me to Python Tutorial - Part 5 Take me to Python Tutorial - Part 7
If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.
Take me to Python Tutorial - Part 5 Take me to Python Tutorial - Part 7