Learn Python: 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.

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]

Key Concepts

Review core concepts you need to learn to master this subject

Python for Loops

dog_breeds = ["boxer", "bulldog", "shiba inu"] # Print each breed: for breed in dog_breeds: print(breed)

Python for loops can be used to iterate over and perform an action one time for each element in a list.

Proper for loop syntax assigns a temporary value, the current item of the list, to a variable on each successive iteration: for <temporary value> in <a list>:

for loop bodies must be indented to avoid an IndentationError.

Python Loops with range().

dog_breeds = ["boxer", "bulldog", "shiba inu"] # Print each breed: for breed in dog_breeds: print(breed)

In Python, a for loop can be used to perform an action a specific number of times in a row.

The range() function can be used to create a list that can be used to specify the number of iterations in a for loop.

The Python break Keyword

dog_breeds = ["boxer", "bulldog", "shiba inu"] # Print each breed: for breed in dog_breeds: print(breed)

In a Python 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!
  • Finished looping

The Python continue Keyword

dog_breeds = ["boxer", "bulldog", "shiba inu"] # Print each breed: for breed in dog_breeds: print(breed)

In Python, the continue keyword is used inside a loop to skip the remaining code inside the loop code block and begin the next loop iteration.

Python While Loops

dog_breeds = ["boxer", "bulldog", "shiba inu"] # Print each breed: for breed in dog_breeds: print(breed)

In Python, a while loop will repeatedly execute a code block as long as a condition evaluates to True.

The condition of a while loop is always checked first before the block of code runs. If the condition is not met initially, then the code block will never run.

Python Nested Loops

dog_breeds = ["boxer", "bulldog", "shiba inu"] # Print each breed: for breed in dog_breeds: print(breed)

In Python, loops can be nested inside other loops. Nested loops can be used to access items of lists which are inside other lists. The item selected from the outer loop can be used as the list for the inner loop to iterate over.

Python List Comprehension

dog_breeds = ["boxer", "bulldog", "shiba inu"] # Print each breed: for breed in dog_breeds: print(breed)

Python list comprehensions provide a concise way for creating lists. It consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses: [EXPRESSION for ITEM in LIST <if CONDITIONAL>].

The expressions can be anything - any kind of object can go into a list.

A list comprehension always returns a list.

Chevron Left Icon
Learn Python: Loops
Lesson 1 of 2
Chevron Right Icon
  1. 1

    Suppose we want to [...] each item from a list of [...] . We would need to use the following code snippet: [...] This seems inefficient. Luckily, Python (and most other programming language...

  2. 2

    In the previous exercise, we saw that we can print each item in a list using a for loop. A for loop lets us perform an action on each item in a list. Using each element of a list is known as _i...

  3. 3

    Previously, we iterated through an existing list. Often we won't be iterating through a specific list, we'll just want to do a certain action multiple times. For example, if we wanted to print out...

  4. 4

    We've iterated through lists that have a discrete beginning and end. However, let's consider this example: [...] What happens here? Every time we enter the loop, we add a [...] to the end of ...

  5. 5

    We often want to use a for loop to search through a list for some value: [...] This code goes through each [...] in [...] and checks for a match. After we find that [...] is in the list ...

  6. 6

    When we're iterating through lists, we may want to skip some values. Let's say we want to print out all of the numbers in a list, unless they're negative. We can use [...] to move to the next [....

  7. 7

    We now have seen and used a lot of examples of for loops. There is another type of loop we can also use, called a while loop. The while loop performs a set of code until some condition is reach...

  8. 8

    We have seen how we can go through the elements of a list. What if we have a list made up of multiple lists? How can we loop through all of the individual elements? Suppose we are in charge of a ...

  9. 9

    Let's say we have scraped a certain website and gotten these words: [...] We want to make a new list, called [...] , that has all of the strings in [...] with an [...] as the first chara...

  10. 10

    Let's say we're working with the [...] list from the last exercise: [...] We want to create a new list with the string [...] added to the end of each [...] . We want to call this new list ...

  11. 11

    Good job! In this lesson, you learned - how to write a for loop - how to use [...] in a loop - what infinite loops are and how to avoid them - how to skip values in a loop - how to write a while...

  1. 1

    This lesson will help you review Python functions by providing some challenge exercises involving loops. As a refresher, function syntax looks like this: [...] For example, a function that pri...

  2. 2

    [...]

  3. 3

    [...]

  4. 4

    [...]

  5. 5

    [...]

  6. 6

    [...]

  7. 7

    [...]

  8. 8

    [...]

  9. 9

    [...]

  10. 10

    [...]

  11. 11

    [...]

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

Learn Python: Loops

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]