Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

One-Line While Loops

Give Feedback

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!

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.

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?

Become a Member to join the conversation.