Mastering For Loops in Python | Lesson 7

For loops in Python are so often used that they are crucial for all novice programmers to master. For loops are one of the two types of loops used in Python, and mastering them can reduce your code length and enhance your programming skills.

Grammarly Writing Support

For loops are more intuitive than the while loop. A for loop essentially says, “For all the items in this sequence, execute this block of code.” The while loop, on the other hand, says while this condition exists, execute this block of code.

In this lesson, we will specifically examine for loops with range, break, and continue.

If you are new to loops in Python, be sure to check out our lesson on loop iteration to explore what happens in a program when a loop is being executed.

For loops in Python with range – one argument

For loops iterate over a sequence, regardless of whether it’s a list, tuple, string, range, or another iterable object.

for i in range (5):
  print (i)

Now, this has a condition that isn’t as clear to see as the one in the while loop. Look at the line for i in range (5):. The single number in the parentheses is called an argument. The for loop can hold multiple arguments.

This one holds one argument, 5. This is a built-in function that returns all numbers from 0 to but not including 5.

So when the program prints i the output looks like this:

0
1
2
3
4

Now, let’s look at one with two arguments.

For loops in Python with range – two arguments

Now let’s look at a Python for loop with the range function, which has two arguments: for i in range(3, 9):. The 3 and 9 in the parentheses are called arguments.

The first argument in this range specifies the starting point, while the last one indicates the stopping point.

for i  in range (3, 9):
  print (i)

The second argument (9) says you need to stop at the number directly before 9. Since the first number is 3, this will be the first number printed.

Here is what this loop prints:

3
4
5
6
7
8

For loops in Python with range – three arguments

Now look at the Python for loop with range below. As you can see, this one has three arguments: 5, 12, and 3.

for i in range (5, 12, 3):
  print (i)

Let’s examine these 3 arguments to understand what is going on in this program. As in the for loop that has two arguments, the first value indicates that this is the first number in the range. The second number indicates that the values must always be less than it.

The third number indicates the number pattern. For instance, if this number is 2, the list begins with the first number counting in 2s and ending at the highest possible value that is lower than the second one.

Here is the output for the code above:

5
8
11

For loops in Python with break

The break statement exits the loop immediately upon completion. Here is an example:

for i in range (6):
    if i == 3:
        break
    print (i)

Now, let’s look at what is happening in this code. The first line for i in range (6) says that i takes the value of this sequence of numbers 0, 1, 2, 3, 4, 5 upon each iteration.

So here is what is happening in the code step by step:

Loop iteration 1:

  1. In the first line, i holds 0.
  2. Check: If i == 3. Since i does not hold the value 3 this evaluates to false.
  3. The program will skip the break.
  4. Print i -> 0 will be printed.

Loop iteration 2:

  1. This time in the first line i holds 1.
  2. Check: If i ==3. Since i does not hold the value 3, this evaluates to false.
  3. The program will skip the break.
  4. Print i -> 1 will be printed.

Loop iteration 3:

  1. In the first line, i holds 2.
  2. Check: If i ==3. Since i does not hold the value 3 this evaluates to false.
  3. The program will skip the break.
  4. Print i -> 2 will be printed.

Loop iteration 4:

  1. In the first line i holds 3.
  2. Check: If i ==3. Since i now holds the value 3 this evaluates to true.
  3. The program will execute break which stops the program.

So the output of this program will be:

0
1
2

For loops in Python with continue

Continue is used to stop the current iteration and proceed to the next. Here is an example:

for i in range (1, 13):
    if i % 2==0:
        continue
    print(f"odd number: {i}")

Let’s look at what is happening in this program line by line.

  1. The range (1, 13) generates the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.
  2. i % 2 == 0: % is the modulo operator, which gives the remainder of a division.
    • So i % 2 checks to see if i divided by 2 leaves a remainder.
    • So if i % 2 = 0, the number in i is an even number.
    • On the other hand, if i % 2 = 1, the number is an odd number.
    • So i % 2 == 0 will evaluate to true for even numbers and false for odd numbers.
  3. Continue acts as a control statement.
    • Each time continue runs it tells the program to skip what comes after and go back to the beginning of the loop.
    • As such, the print statement in this program will not run when continue is executed. Instead, the program will go back to the beginning of the loop.
  4. Now for the last line print(f”odd number: {i}”)
    • This line prints whatever is contained in i. However, it will only print odd numbers because whenever i is an even number, continue will cause the loop to go back up.

Program execution

Now let’s look at what happens when this program is executed:

Iteration 1:

  1. i = 1
  2. So i % 2 = 1
  3. As you can see, the line if i % 2==0: is false, so continue will be skipped.
  4. Odd number: 1 will be printed.

Iteration 2:

  1. i = 2
  2. So i % 2 = 0
  3. As you can see, the line if i % 2==0: is true, so continue will cause the program to return to the beginning of the loop.

Iteration 3:

  1. i = 3
  2. So i % 2 = 1
  3. As you can see, the line if i % 2==0: is false, so continue will be skipped.
  4. Odd number: 3 will be printed.

Iteration 4:

  1. i = 4
  2. So i % 2 = 0
  3. As you can see, the line if i % 2==0: is true, so it will cause the program to return to the beginning of the loop.

Iteration 5:

  1. i = 5
  2. So i % 2 = 1
  3. As you can see, the line if i % 2==0: is false, so continue will be skipped.
  4. Odd number: 5 will be printed.

Iteration 6:

  1. i = 6
  2. So i % 2 = 0
  3. As you can see, the line if i % 2==0: is true, so it will cause the program to return to the beginning of the loop.

The program will continue in this manner until the last odd number before 13 is printed. Here is what the output will look like:

odd number: 1
odd number: 3
odd number: 5
odd number: 7
odd number: 9
odd number: 11

Before you go

In this lesson we explored the for look with range(). We looked at range with one, two and three arguments. We also looked at different ways to exit a loop early.

Mastering these coding structures will help you to write better Python codes.

We have many Python lessons designed to improve your expertise. Be sure to check out the other lessons.

Leave a Comment