Python break Keyword

Example

End the loop if i is greater than 5:

for i in range(9):
  if i > 5:
    break
  print(i)

Run Instances

Definition and Usage

The break keyword is used to break out of for loops or while loops.

More Examples

Example

Break while loop:

i = 1
while i < 9:
  print(i)
  if i == 5:
    break
  i += 1

Run Instances

Related Pages

Use continue Keyword You can end the current iteration in the loop but continue with the next one.

Read more about for loops in our Python For Loop Tutorial Read more about for loops in our

Read more about for loops in our Python While Loop Tutorial Read more about while loops in the Chinese version.