You (Level 0)
0% Complete
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.

For a more tangible and better look into the Python language, consider reading the following book. It's an excellent read.

Your code will execute in this window.

Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

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

## Comment on

• #### mindwars Jan. 18, 2017, 1:46 a.m.

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

• #### AymenNajar Dec. 25, 2016, 5:17 p.m.

question: why do we have t put the a+=1? a = 1 while a < 10: print (a) a+=1

• #### AymenNajar Dec. 25, 2016, 5:07 p.m.

quetsion: why do we have t put the a+=1? a = 1 while a < 10: print (a) a+=1

• #### puermaster June 27, 2015, 2:57 p.m.

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?

• #### oren123 April 6, 2015, 9:47 p.m.

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

• #### oren123 April 6, 2015, 9:47 p.m.

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

• #### oren123 April 6, 2015, 9:47 p.m.

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

• #### Haikun March 14, 2015, 4:04 a.m.

Infinite loop because the space blocks incorrect. a = 1 while a < 10: print (a) a+=1 4 spaces for each new line.

• #### Haikun March 14, 2015, 4:04 a.m.

Infinite loop because the space blocks incorrect. a = 1 while a < 10: print (a) a+=1 4 spaces for each new line.

• #### Haikun March 14, 2015, 4:04 a.m.

Infinite loop because the space blocks incorrect. a = 1 while a < 10: print (a) a+=1 4 spaces for each new line.

• #### foxnoname March 2, 2015, 8:04 p.m.

Oh, I wondered why tab wasn't working.

• #### foxnoname March 2, 2015, 8:03 p.m.

a = 4 while a < 10: print (a) a+=(1)

• #### Craig Jan. 16, 2015, 9:56 p.m.

The code here will cause browser to crash. Spacing incorrect. It should be a = 1 while a < 10: print (a) a+=2

Good lesson.

• #### Landazar Jan. 9, 2015, 5:04 p.m.

never mind, I forgot to indent a+=1

• #### Landazar Jan. 9, 2015, 5:02 p.m.

a = 1 while a < 10: print (a) a+=1 this produces an infinite loop of 1 for me and im not sure why

• #### Barak Dec. 8, 2014, 2:14 p.m.

wow! infinite loops! . . . and this is how my chrome died

• #### Neo Dec. 5, 2014, 3:45 p.m.

the code is lesser than the one in C

• #### hushuaiqi Nov. 19, 2014, 2:04 a.m.

esay to hand on ,simple and elegent

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

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

very useful!!!

• #### JacobzoneSept. 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;

• #### NaveenJuly 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

• #### MrMatrixJune 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 .

• #### Cosmo Sutherland McCookJune 23, 2014, 11:41 p.m.

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

• #### peterithJune 19, 2014, 5:25 p.m.

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

• #### Arunkumar RavikumarMay 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;)

• #### Madasyn CaplinMay 26, 2014, 12:14 p.m.

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

• #### Luis ArceMay 23, 2014, 5:26 a.m.

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

• #### JamieApril 23, 2014, 11:27 p.m.

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

• #### Andy MApril 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!

• #### Michael DMarch 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?

• #### rousbel_villarMarch 5, 2014, 5:24 a.m.

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

• #### Lotfi GHAZOUANIFeb. 26, 2014, 1:54 p.m.

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

• #### XenosFeb. 25, 2014, 7:48 p.m.

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

• #### Long Tall TexanDec. 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.

• #### RubenOct. 15, 2013, 8:19 p.m.

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

• #### RubenOct. 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.

• #### RebarakazSept. 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 :)

• #### DarrenAug. 28, 2013, 2:16 a.m.

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

• #### AmbrotosJune 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!

• #### Daniel WiliantoJune 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.

• #### mbogelundApril 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 :-)

• #### ArchieApril 4, 2013, 10:29 a.m.

Typos now gone - well done.

• #### Anton MillikenFeb. 27, 2013, 7:13 p.m.

Wow. The infinite loop thing will be useful.