Newest Visitor Badge Earned!
Click to create an account and start competing today!
After Hours Programming
Show Tutorials


You (Level 0)
0/25
Last Badge Earned
Newest Visitor

Python While Loop Tutorial

Python While Loop

The Python While Loop tutorial explains the use of while loops in python.

While loops in Python can be extremely similar to the for loop if you really wanted them to be. Essentially, they both loop through for a given number of times, but a while loop can be more vague (I'll discuss this a little bit later). Generally, in a while loop you will have a conditional followed by some statements and then increment the variable in the condition. Let's take a peek at a while loop really quick:

Example a = 1
while a < 10:
print (a)
a+=1
Result 1
2
3
4
5
6
7
8
9

Fairly straightforward here, we have our conditional of a < 10 and a was previously declared and set equal to 1. So, our first item printed out was 1, which makes sense. Next, we increment a and ran the loop again. Of course, once a becomes equal to 10, we will no longer run through the loop.

The awesome part of a while loop is the fact that you can set it to a condition that is always satisfied like 1==1, which means the code will run forever! Why is that so cool? It's awesome because you create listeners or even games. Be warned though, you are creating an infinite loop, which will make any normal programmer very nervous.

Where's the do-while loop

Simple answer, it isn't in Python. You need to consider this before you are writing your loops. Not that a do-while loop is commonly used anyway, but Python doesn't have any support for it currently.

Try testing your code with the code simulator!

Let's move on to some more tutorials or the next section!



Previous Tutorial

For Loop

Next Tutorial

Strings







If you enjoyed this resource, support me by sharing this page with others.

Stumbleupon Facebook Twitter GooglePlus Reddit Delicious Digg


Comment or Suggestion?

Comments are a way to tell me about corrections or suggestions, what you thought about the resource, or providing additional information. Help feed the discussion!


Yes




Comments on Python While Loop

user
Naveen
100
July 5, 2014 02:43PM
How would you do  NOT Greater than in Python? 

This web based Interpreter does not do  
a = 1
while a !> 10:
print (a)
a+=1


user
MrMatrix
100
June 29, 2014 03:48PM
Ok guys im going try to explain in more detail what A+=1 is doing.
Equal sign gives a value to a Letter.
First you introduce A.         A = 5
To A im going to add 5. so  A += 5
After you introduce A to a value. Your next code line should be what you are adding to A. Type print(A) to check your answer .




user
Cosmo Sutherland McCook
450
June 23, 2014 11:41PM
a=1
while a ==1:
    print (a)
    a+=1


user
peterith
125
June 19, 2014 05:25PM
a=1
while a<10:
    print(a)
    a+=1


user
Arunkumar Ravikumar
100
May 30, 2014 11:46PM
Also i was surprised to see have the value of  variable "a" is used , good one
for a in range(1,10):
    print("Loop is running for "+str(a)+"th time")
    while a <= 10:
        print(a)
        a+=1
    print("=============")
        


user
Madasyn Caplin
100
May 26, 2014 12:14PM
Try this one:
b = 2
while b < 10:
    print(b)
b+=2

Its really cool! :{P


user
Luis Arce
100
May 23, 2014 05:26AM
What's with the a+=1? I see no explanation on this.


user
ABIN VARGHESE
April 24, 2014 05:10AM
This is really helpful.... Thanks a lot


user
Jamie
375
April 23, 2014 11:27PM
Thanks to Allen Prattis (March 24, 2014) for the tip.


user
Andy M
150
April 3, 2014 08:48PM
Could you possibly include a way to stop the code from running somehow? I'm sure I'm not the only one to have accidentally created an infinite loop...
Thanks!


user
Allen Prattis
March 24, 2014 04:59PM
Typing the following into Test Code works:

a = 1
while a <= 100:
    print (a)
    a+=1

Typing the following into Test Code seems to loop forever:

a = 1
while a <= 100:
    print (a)
a+=1 #No spaces before the line begin.





user
holy_sheep
March 7, 2014 07:22PM
how to understand the "Why is that so cool? It's awesome because you create listeners or even games"?I am new for programming.


user
Michael D
125
March 5, 2014 07:19PM
 Code simulator isn't working for me. Input
a=26
while a > 53:
    print (a)
    a+=26
Hit test code nothing appears. Fixes? Anybody else have problem?


user
rousbel_villar
150
March 5, 2014 05:24AM
Awesome this was very interesting , specially the condition a==x.


user
Lotfi GHAZOUANI
200
February 26, 2014 01:54PM
This example is like the  for in range loop. Another syntax to do with in Python.


user
Xenos
150
February 25, 2014 07:48PM
I did not listen, and tried the infinite loop....almost got my PC fried lol


user
Long Tall Texan
125
December 22, 2013 12:49PM
The great thing about these tutorials is that you can jump right in and start learning the basics without downloading software. The code simulator is a great idea.


user
Damaris
November 10, 2013 01:08PM
Good explanations.


user
Ruben
175
October 15, 2013 08:19PM
Never mind my last comment I saw the error of my way! Indentions!


user
Ruben
175
October 15, 2013 08:10PM
I am not able to get the While Loop example to work on Python 3.3.2 Interpreter. Typed in exactly like the example, but I get the following error after clicking 'Enter' at end of Line 3: File "<stdin>", line 2.  Line 2 looks like this: while a < 10:
Thanks.


user
Rebarakaz
250
September 14, 2013 04:17AM
Seems like that the examples here didn't running well in Python 3.3.2. I have to press the ENTER button twice to get the results. Am I missing something here?
Great tutorials so far and it's easy as 123 :)


user
Darren
125
August 28, 2013 02:16AM
While loop is useful but if you forget a conditional, problems lol


user
John
July 7, 2013 08:24AM
when testing the do-while loop I created, python became unresponsive. It was the same exact program in the test example


user
Ambrotos
100
June 28, 2013 12:12PM
There is a typo on the python while loop page. "creating infinite loop" should be " creating an infinite loop".  Great tutorial so far though, thank you!


user
Daniel Wilianto
225
June 5, 2013 12:23AM
@Joe : I believe that using "break" is not a good programming practice. It will be better to create a boolean variable, set to our for/while's conditional sentence, and set it to True inside the loop whenever a certain condition is satisfied.


user
Joe Bigler
June 1, 2013 04:26PM
#There are two types of Do loops.  One checks the condition at the beginning of the loop and the second checks the condition at the end.  Here's what I think would work in Python:

index = 0
list = []
#Test the condtion at the end of the loop.  This loop always runs once.
while True: 
    list.append(index)
    index = index +1	
    if index > 10:break  #Test the condition.

print(list)

index = 0
list = []
#Test the condtion at the beginning of the loop.  This loop will not run at all if the initial condtion is false.

while True: 
    if index > 10:break  #test the condition
    list.append(index)
    index = index +1	
    
print(list)

#The last one could be a While loop.  Notice the test changes

index = 0
list = []
#Test the condtion at the beginning of the loop.  This loop will not run at all if the initial condtion is false.

while index <= 10:  #test the condition
    list.append(index)
    index = index +1	
    
print(list)


user
mbogelund
50
April 12, 2013 11:03PM
The sentence "The awesome part of a while loop is the fact that you can set it to a condition that can never be satisfied like 1==1" doesn't make sense, since 1==1 is ALWAYS satisfied.
Please correct :-)


user
Archie
125
April 4, 2013 10:29AM
Typos now gone - well done.


user
Archie
April 1, 2013 11:35AM
I like your style, but there are typos around - the last 3 times you say a<10 here, you mean just a.


user
Anton Milliken
175
February 27, 2013 07:13PM
Wow. The infinite loop thing will be useful.