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 Loops with range()
.
Infinite Loop
Python while
Loops
Python Nested 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
- 1How do we use code to tell a computer this: “Create a variable and call a function 15 times”? We could write it out 15 times: create a variable call a function create a variable call a function …
- 3What if you approached tile-placing another way: you don’t know how many tiles to place, but you know when to stop. How could you communicate this type of command to a computer? Give it the instruc…
- 4There’s one more way to give looping instructions to a computer. We define a sequence of desired tile colors (a list), and tell the computer to repeat the instructions for each item in the sequen…
- 5Let’s combine all that we’ve learned so far: * variables, operators, and data types * functions and control flow * lists and loops Examples of for and while loops are provided below in JavaScript….