Loops
Loops are structures that let you repeat Python code over and over. Learn how to read loops and write them to solve your own problems.
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 1
- 1In our everyday lives, we tend to repeat a lot of processes without noticing. For instance, if we want to cook a delicious recipe, we might have to prepare our ingredients by chopping them up. We…
- 2Before we get to writing our own loops, let’s explore what programming would be like if we couldn’t use loops. Let’s say we have a list of ingredients and we want to print every element in the lis…
- 3Now that we can appreciate what loops do for us, let’s start with your first type of loop, a for loop, a type of definite iteration. In a for loop, we will know in advance how many times the loo…
- 4Often we won’t be iterating through a specific list (or any collection), but rather only want to perform a certain action multiple times. For example, if we wanted to print out a “Learning Loops!”…
- 5In Python, for loops are not the only type of loops we can use. Another type of loop is called a while loop and is a form of indefinite iteration. A while loop performs a set of instructions as l…
- 6A while loop isn’t only good for counting! Similar to how we saw for loops working with lists, we can use while loops to iterate through a list as well. Let’s return to our ingredient list: ingr…
- 7We’ve iterated through lists that have a discrete beginning and end. However, let’s consider this example: my_favorite_numbers = [4, 8, 15, 16, 42] for number in my_favorite_numbers: my_favorit…
- 8Loops in Python are very versatile. Python provides a set of control statements that we can use to get even more control out of our loops. Let’s take a look at a common scenario that we may encou…
- 9While the break control statement will come in handy, there are other situations where we don’t want to end the loop entirely. What if we only want to skip the current iteration of the loop? Let’…
- 10Loops can be nested in Python, as they can with other programming languages. We will find certain situations that require nested loops. Suppose we are in charge of a science class, that is split i…
- 11So far we have seen many of the ideas about using loops in our code. Python prides itself on allowing programmers to write clean and elegant code. We have already seen this with Python giving us th…
- 12List Comprehensions are very flexible. We even can expand our examples to incorporate conditional logic. Suppose we wanted to double only our negative numbers from our previous numbers list. We…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory