While Loop

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

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.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Comment

  1. 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 🙂

  2. @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.

  3. 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!

  4. 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 🙂

  5. 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.

  6. 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.

  7. 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?

  8. 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!

  9. 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("=============")

  10. 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 .

  11. 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

  12. 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
    >>>

  13. 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…

  14. 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?

  15. 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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.