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:
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.
Wow. The infinite loop thing will be useful.
just wondering why the 1 is indented an the others arent on your results. i dont get that indent in the results here, either on your tester or on IDLE
question:
why do we have t put the a+=1?
a = 1
while a < 10:print (a)a+=1
quetsion:
why do we have t put the a+=1?
a = 1
while a < 10:print (a)a+=1
Hi! I’ve run the code here and in a couple of IDEs, and it just prints loads of 1s, vertically. What am I doing wrong?
a = 1
while a < 9999999999999999999999:print (a) a+=1
a = 1
while a < 9999999999999999999999:print (a) a+=1
a = 1
while a < 9999999999999999999999:print (a) a+=1
Infinite loop because the space blocks incorrect.
a = 1
while a < 10: print (a) a+=14 spaces for each new line.
Infinite loop because the space blocks incorrect.
a = 1
while a < 10: print (a) a+=14 spaces for each new line.
Infinite loop because the space blocks incorrect.
a = 1
while a < 10: print (a) a+=14 spaces for each new line.
Oh, I wondered why tab wasn’t working.
a = 4
while a < 10: print (a) a+=(1)
The code here will cause browser to crash. Spacing incorrect. It should be
a = 1
while a < 10: print (a) a+=2
Good lesson.
never mind, I forgot to indent a+=1
a = 1
while a < 10: print (a) a+=1this produces an infinite loop of 1 for me and im not sure why
wow! infinite loops!
.
.
.
and this is how my chrome died
the code is lesser than the one in C
esay to hand on ,simple and elegent
Okay, you lost me at Loops. But it’s probably because it’s 1:26 AM, and I did not bother searching the meaning of some specific terms…
very useful!!!
This is my example (Gone Wrong?) I Need help xD
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> a = 1
>>> while a <30:
print("a")
a+=1
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
>>>
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
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 .
a=1
while a ==1:
print (a)
a+=1
a=1
while a<10:
print(a)
a+=1
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("=============")
Try this one:
b = 2
while b < 10:
print(b)
b+=2
Its really cool! :{P
What’s with the a+=1? I see no explanation on this.
Thanks to Allen Prattis (March 24, 2014) for the tip.
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!
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?
Awesome this was very interesting , specially the condition a==x.
This example is like the for in range loop. Another syntax to do with in Python.
I did not listen, and tried the infinite loop….almost got my PC fried lol
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.
Never mind my last comment I saw the error of my way! Indentions!
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.
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 🙂
While loop is useful but if you forget a conditional, problems lol
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!
@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.
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 🙂