When an external condition is met, you can exit a loop using the break statement in Python. You’ll put the break statement within the block of code under your loop statement, usually after a conditional if statement. When the break statement is met, code interpreter exits the current loop and continues with next line of code right after the loop.


Here is a sample usage of the break statement in action:

number = 0

for number in range(10):
   number = number + 1

   if number == 5:
      break    # break here

   print('Number is ' + str(number))

print('Exited loop at {0}'.format( number ))

Here is the output of this sample code:

Number is 1
Number is 2
Number is 3
Number is 4
Exited loop at 5