Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while True vs while {condition}

I'm reading a book on Python3 (Introducing Python by Bill Lubanovic), and came across something I wasn't sure is a Python preference, or just a "simplification" due to being a book and trying to describe something else.

It's on how to write to a file using chunks instead of in one shot.

poem = '''There was a young lady named Bright,
Whose speed was far faster than light;
She started one day
In a relative way,
And returned on the previous night.'''

fout = open('relativity', 'wt')
size = len(poem)
offset = 0
chunk = 100
while True:
    if offset > size:
        break
    fout.write(poem[offset:offset+chunk])
    offset += chunk
fout.close()

I was about to ask why it has while True instead of while (offset > size), but decided to try it for myself, and saw that while (offset > size) doesn't actually do anything in my Python console.

Is that just a bug in the console, or does Python really require you to move the condition inside the while loop like that? With all of the changes to make it as minimal as possible, this seems very verbose.

(I'm coming from a background in Java, C#, and JavaScript where the condition as the definition for the loop is standard.)

EDIT

Thanks to xnx's comment, I realized that I had my logic incorrect in what I would have the condition be.

This brings me back to a clearer question that I originally wanted to focus on:

Does Python prefer to do while True and have the condition use a break inside the loop, or was that just an oversight on the author's part as he tried to explain a different concept?

like image 345
krillgar Avatar asked Apr 06 '26 18:04

krillgar


1 Answers

I was about to ask why it has while True instead of while (offset <= size), but decided to try it for myself,

This is actually how I would have written it. It should be logically equivelent.

and saw that while (offset > size) doesn't actually do anything in my Python console.

You needed to use (offset <= size), not (offset > size). The current logic stops as soon as the offset is greater than size, so reverse the condition if you want to put it in the while statement.

does Python really require you to move the condition inside the while loop like that?

No, Python allows you write write the condition in the while loop directly. Both options are fine, and it really is more a matter of personal preference in how you write your logic. I prefer the simpler form, as you were suggesting, over the original author's version.

This should work fine:

while offset <= size:
    fout.write(poem[offset:offset+chunk])
    offset += chunk

For details, see the documentation for while, which specifically states that any expression can be used before the :.


Edit:

Does Python prefer to do while True and have the condition use a break inside the loop, or was that just an oversight on the author's part as he tried to explain a different concept?

Python does not prefer while True:. Either version is fine, and it's completely a matter of preference for the coder. I personally prefer keeping the expression in the while statement, as I find the code more clear, concise, and maintainable using while offset <= size:.

like image 106
Reed Copsey Avatar answered Apr 08 '26 09:04

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!