how to break a while loop in python code example

Example 1: how to write a while statement in python

myvariable = 10
while myvariable > 0:
  print(myvariable)
  myvariable -= 1

Example 2: while loop python

while (condition):
  doThis();

Example 3: python get out of loop

while True:
  print('I run!')
  break
print('Not in loop!')

Example 4: python while continue

## When the program execution reaches a continue statement, 
## the program execution immediately jumps back to the start 
## of the loop.
while True:
    print('Who are you?')
    name = input()
    if name != 'Joe':
        continue
    print('Hello, Joe. What is the password? (It is a fish.)')
    password = input()
    if password == 'swordfish':
        break
print('Access granted.')

Example 5: python exit loop iteration

alphabet = ['a' , 'b' , 'c' , 'd' ]
for letter in alphabet:
  if letter == 'b' :
    continue
    #continues to next iteration
  print( letter )
for letter in alphabet:
  if letter == 'b' :
    break
    #terminates current loop
  print( letter )
for letter in alphabet:
  if letter == 'b' :
    pass
    #does nothing
  print( letter )

Example 6: break while loop python

1n = 5
 2while n > 0:
 3    n -= 1
 4    if n == 2:
 5        break
 6    print(n)
 7print('Loop ended.')