logic – Python while (bool):

logic – Python while (bool):

while swag: will run while swag is truthy, which it will be while swag is True, and will not be when you set swag to False.

Does while swag – check if swag exists or if swag is True

It checks if swag is True (or truthy, I should say). And yes, the loop will exit after 3 iterations because i=i+1 must be executed 3 times until i == 3 and (by the if-statement) swag is set to False, at which point the loop will exit.

But why not check this yourself?

swag = True
i = 0

while swag:
    i=i+1
    print(swag)
    if i == 3:
        swag = False
True
True
True

logic – Python while (bool):

You can also shorten your expression to increment variable i by 1 by using the following notation:
i+=1 (same as i=i+1)

Leave a Reply

Your email address will not be published.