Key Concepts

Review core concepts you need to learn to master this subject

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!
Lesson 1 of 1
  1. 1
    In 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…
  2. 2
    Before 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…
  3. 3
    Now 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…
  4. 4
    Often 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!”…
  5. 5
    In 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…
  6. 6
    A 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…
  7. 7
    We’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…
  8. 8
    Loops 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…
  9. 9
    While 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’…
  10. 10
    Loops 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…
  11. 11
    So 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…
  12. 12
    List 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…
  13. 13
    Good job! In this lesson, you learned - How to write a for loop. - How to use range in a loop. - How to write a while loop. - What infinite loops are and how to avoid them. - How to control loops …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo