CS101 Loops
Learn how to create loops in Python.
StartKey Concepts
Review core concepts you need to learn to master this subject
break
Keyword
Python List Comprehension
Python For Loop
The Python continue
Keyword
Python for
Loops
Python Loops with range()
.
Infinite Loop
Python while
Loops
break
Keyword
break
Keyword
numbers = [0, 254, 2, -1, 3]
for num in numbers:
if (num < 0):
print("Negative number detected!")
break
print(num)
# 0
# 254
# 2
# Negative number detected!
In a loop, the break
keyword escapes the loop, regardless of the iteration number. Once break
executes, the program will continue to execute after the loop.
In this example, the output would be:
0
254
2
Negative number detected!
Loops
Lesson 1 of 2