Example exit for loop in Python
Simple example code use break statement after a conditional
if statement in for loop. If the number is evaluated as equivalent to 2, then the loop breaks.
number = 0 for number in range(10): if number == 2: break # break here print('Number is ' + str(number)) print('Out of loop')
Python exit loop iteration.
alphabet = ['a', 'b', 'c', 'd'] for letter in alphabet: if letter == 'b': break # terminates current loop print(letter)
Do comment if you have any doubts and suggestions on this Python for loop topic.
Note: IDE: PyCharm 2021.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.