Python Break


Python break Statement

The break statement is used to terminate the for loop or the while loop.

For this, we use the keyword break.

Break with for loop

The break will terminate the for loop on meeting the specific condition.

For example, in the below code we will iterate from the string and in case letter ‘a’ is found in the string, then it will break the loop.

Loop break at letter ‘a’.

In [1]:
name = "Logical"
for letter in name:
    if letter == 'a':
        break
    print(letter)
L
o
g
i
c

Break with while loop

Same way, we can terminate the while loop using the break statement.

For example, here, we will break the while loop when the value of i = 3.

Loop break at number 3.

In [1]:
i = 0

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

Resources

Handwritten Notes