While loop for beginners

hi guys… so is it that the while loop does not involve an else command at the end. or in some cases you can use else after the while loop?

while can always have a succeeding else statement, but it doesn’t have to. If there’s an else block, it will be run if the while-loop is exited due to its condition (ie the a < 5 part of while a < 5:). If the while-loop is exited for any other reason (eg exited via break, or exited due to exception), then the else block won’t be executed. See the documentation

For example, this doesn’t print “spam”:

a = 0
while a < 5:
    a += 1
    break
else:
    print("spam")

But this does print “spam”:

a = 0
while a < 5:
    a += 1
else:
    print("spam")