Home » Tutorials » Python » While Loop

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



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "While Loop". After Hours Programming. Accessed on March 16, 2024. https://www.afterhoursprogramming.com/tutorial/python/while-loop-py/.

  • Stewart, Suzy. "While Loop". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/python/while-loop-py/. Accessed 16 March, 2024.

  • Stewart, Suzy. While Loop. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/python/while-loop-py/.



44 thoughts on “While Loop”

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

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

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

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

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

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

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

Leave a Comment

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