One-Line While Loops
This lesson covers the possibility to write one-line while
-loops. It also covers the limitations of this approach. You’ll find the example used in this video below.
a = 5
while a > 0: a = a - 1; print(a)
00:00
All right. So now that we’ve seen how complicated some while
loops can be by nesting, let’s look at how simple they can be by just looking at a single-line while
loop.
00:11
We’re going to have a variable a
. It’s going to be equal to 5
. And now a single-lined while
loop starts off the same way, with a while
, and then it has our condition that needs to be met.
00:23
So let’s say that a
needs to be greater than 0
. And then you’re still going to have your colon (:
) here. And at that point, you just continue to type out what you want to happen over each iteration.
00:38
So first, I’m going to take a
and I’m going to add
00:45
+ 1
. And then your second statement is going to be separated with a semicolon (;
). So my second thing I want it to do is to print(a)
.
00:58
Oop, ha. This should be a minus (-
), because that would go on forever.
01:06
All right, and that’s it! Let’s see what happens. Here we go! So my while
loop still works but I have it all on one line. Now, one thing I do want to point out—I cannot have a nested statement in a single-line while
loop.
01:24
So I couldn’t say if
, you know, a == 2
and then have another thing that says print(a)
.
01:44 This isn’t going to work. So when I try to run that, I get an error, and that is because I have this—you can’t put this nested conditional statement in a single-line block.
01:58
But all right! That pretty much wraps up our tutorial on while
loops. If you would like to find out more information, you can always visit our resources on Real Python, but I hope you got some good information and learned all about while
loops!
MiguelH on March 13, 2019
Kathy, I got the point of nested whiles. Does it mean that as far as you place a nested statement it always will be exhausted before printing the previous one?
Dan Bader RP Team on March 14, 2019
@MiguelH: Yep, that’s right. If you have a nested while loop like this:
while condition_a:
# (do something)
while condition_b:
# (do something)
The outer loop (the one with condition_a
) only iterates after the inner loop (condition_b
) has been completed (or aborted with break
). Does that answer your question?
Sebastiaandb on Feb. 5, 2024
Hi Katy! Great explanation! I was wondering what code editor do you use? I’m new (as most of us here i guess) with Python and am using Visual Studio Code but the debugger in there doesn’t give me all the insights you have. VS code does give the variable/object value at every loop but doesn’t give me insights in for example the value of “True/False” per loop.
Martin Breuss RP Team on Feb. 5, 2024
Hi @Sebastiaandb, Katy is using an editor called Thonny that’s specifically designed to be approachable for learning Python!
You can learn more about it in our tutorial called Thonny: The Beginner-Friendly Python Editor :)
Sebastiaandb on Feb. 5, 2024
Thanks Martin! Downloaded it, it’s really nice for beginners like me!
Become a Member to join the conversation.
Bill on March 13, 2019
Katy, Really well done, made while loops readily understandable. Thank you.
I changed the fizz, baz, buzz to one, two, three, just so I could more readily identify where I was and what was going on. Probably more of my dyslexia than anything else.