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

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)

Comments & Discussion

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.