Skip to Content


You (Level 0)
0% Complete
Last Badge Earned
None

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.

Let's explore some more tutorials or topics!

Previous Tutorial Next Tutorial


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

Comment on

  • User

    ExpandX Oct. 25, 2014, 10:27 p.m.

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

  • User

    Eleanor Oct. 22, 2014, 1:41 p.m.

    very useful!!!

  • User

    Jacobzone Sept. 10, 2014, 6:52 p.m.

    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 &quot;copyright&quot;, &quot;credits&quot; or &quot;license()&quot; for more information. &gt;&gt;&gt; a = 1 &gt;&gt;&gt; while a &lt;30: print(&quot;a&quot;) 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 &gt;&gt;&gt;

  • User

    Naveen July 5, 2014, 2:43 p.m.

    How would you do NOT Greater than in Python? This web based Interpreter does not do a = 1 while a !&gt; 10: print (a) a+=1

  • User

    MrMatrix June 29, 2014, 3:48 p.m.

    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

    peterith June 19, 2014, 5:25 p.m.

    a=1 while a&lt;10: print(a) a+=1

  • User

    Arunkumar Ravikumar May 30, 2014, 11:46 p.m.

    Also i was surprised to see have the value of variable &quot;a&quot; is used , good one for a in range(1,10): print(&quot;Loop is running for &quot;+str(a)+&quot;th time&quot;) while a &lt;= 10: print(a) a+=1 print(&quot;=============&quot;)

  • User

    Madasyn Caplin May 26, 2014, 12:14 p.m.

    Try this one: b = 2 while b &lt; 10: print(b) b+=2 Its really cool! :{P

  • User

    Luis Arce May 23, 2014, 5:26 a.m.

    What's with the a+=1? I see no explanation on this.

  • User

    Jamie April 23, 2014, 11:27 p.m.

    Thanks to Allen Prattis (March 24, 2014) for the tip.

  • User

    Andy M April 3, 2014, 8:48 p.m.

    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

    Michael D March 5, 2014, 7:19 p.m.

    Code simulator isn't working for me. Input a=26 while a &gt; 53: print (a) a+=26 Hit test code nothing appears. Fixes? Anybody else have problem?

  • User

    rousbel_villar March 5, 2014, 5:24 a.m.

    Awesome this was very interesting , specially the condition a==x.

  • User

    Lotfi GHAZOUANI Feb. 26, 2014, 1:54 p.m.

    This example is like the for in range loop. Another syntax to do with in Python.

  • User

    Xenos Feb. 25, 2014, 7:48 p.m.

    I did not listen, and tried the infinite loop....almost got my PC fried lol

  • User

    Long Tall Texan Dec. 22, 2013, 12:49 p.m.

    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

    Ruben Oct. 15, 2013, 8:19 p.m.

    Never mind my last comment I saw the error of my way! Indentions!

  • User

    Ruben Oct. 15, 2013, 8:10 p.m.

    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 &quot;&lt;stdin&gt;&quot;, line 2. Line 2 looks like this: while a &lt; 10: Thanks.

  • User

    Rebarakaz Sept. 14, 2013, 4:17 a.m.

    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 Aug. 28, 2013, 2:16 a.m.

    While loop is useful but if you forget a conditional, problems lol

  • User

    Ambrotos June 28, 2013, 12:12 p.m.

    There is a typo on the python while loop page. &quot;creating infinite loop&quot; should be &quot; creating an infinite loop&quot;. Great tutorial so far though, thank you!

  • User

    Daniel Wilianto June 5, 2013, 12:23 a.m.

    @Joe : I believe that using &quot;break&quot; 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

    mbogelund April 12, 2013, 11:03 p.m.

    The sentence &quot;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&quot; doesn't make sense, since 1==1 is ALWAYS satisfied. Please correct :-)

  • User

    Archie April 4, 2013, 10:29 a.m.

    Typos now gone - well done.

  • User

    Anton Milliken Feb. 27, 2013, 7:13 p.m.

    Wow. The infinite loop thing will be useful.