Python Cheat Sheet 4
Python Cheat Sheet 4
Loops
break Keyword
In a loop, the break keyword escapes the loop,
regardless of the iteration number. Once break numbers = [0, 254, 2, -1, 3]
executes, the program will continue to execute after the
loop. for num in numbers:
In this example, the output would be: if (num < 0):
print("Negative number detected!")
0
break
254
print(num)
2
# 0
Negative number detected!
# 254
# 2
# Negative number detected!
1 of 3 30/12/2021, 2:06 pm
Firefox https://www.codecademy.com/learn/learn-python-3/modules/learn-pytho...
In�nite Loop
An in�nite loop is a loop that never terminates. In�nite
loops result when the conditions of the loop prevent it
from terminating. This could be due to a typo in the
conditional statement within the loop or incorrect logic.
To interrupt a Python program that is running forever,
press the Ctrl and C keys together on your keyboard.
2 of 3 30/12/2021, 2:06 pm
Firefox https://www.codecademy.com/learn/learn-python-3/modules/learn-pytho...
3 of 3 30/12/2021, 2:06 pm