Intro to Python

11. Loops

Suppose you wanted to print the numbers 0-9. Here’s one way you could do it:

print("0")
print("1")
print("2")
print("3")
print("4")
print("5")
print("6")
print("7")
print("8")
print("9")
> 0
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9

While this code works, it doesn’t scale. If you wanted to instead print the numbers 0-999, it would take a lot of time, effort, and characters to repeatedly print using the same method.

What we can do instead is use a loop. Loops are another method to control the flow of a program when you want to run the same block of code multiple times, but only want to code it once. This code does the exact same thing as the previous 10 lines.

for i in range(10):
    print(i)
> 0
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9

For-loops

The previous example used a type of loop called a for-loop. The syntax to a for-loop that executes a block of code x times looks like this:

for i in range(x):
    # code block

In Python, the for-loop’s code block is simply executed the number of times specified between the parentheses of the range function.

If we wanted to loop through the numbers within a range starting with x and ending with y, we could do

for i in range (x, y):
     # code block

Here's an example if we wanted to print out all the numbers between 10 and 20.

for i in range (10, 20):
  print(i)
> 10
> 11
> 12
> 13
> 14
> 15
> 16
> 17
> 18
> 19

Note that 20 won't be printed in the above example because the endpoint is excluded from the loop.

While loops

While loops are simpler to write and understand compared to for-loops.

The syntax for a while loop looks like this:

while statement:
    # code block

A while loop executes the code block repeatedly while the statement is true. It controls the flow of a program in two steps:

  1. Check if the statement is true. If the statement is true, proceed to step 2. If the statement is false, exit the while loop
  2. Execute the code block and return to step 1

Let’s follow how a while loop executes a similar program to our for-loop demonstration.

i = 0
while i<2:
  print("The code block was executed")
  i+=1
  1. The computer runs all the code before the while-loop, which creates a variable i that will determine when we exit the while-loop.
  2. The computer is on step 1. i currently has value 0, so the boolean i < 2 is true, and we proceed to step 2.
  3. The computer is on step 2. The code block is executed. This prints “The code block was executed,” and i is incremented by 1 (now has value 1). The computer returns to step 1.
  4. The computer is on step 1. i currently has value 1, so the boolean i < 2 is true, and we proceed to step 2.
  5. The computer is on step 2. The code block is executed. This prints “The code block was executed,” and i is incremented by 1 (now has value 2). The computer returns to step 1.
  6. The computer is on step 1. i currently has value 2, so the boolean i < 2 is false, and we exit the while-loop.

Common Mistake: The simplicity of while loops comes at a cost: it is much easier to accidentally create an infinite loop using a while loop since it doesn’t force you to create exit conditions. Functionally, however, for-loops and while-loops are about the same.

In some cases, you may want to create an infinite / very long loop intentionally. For example, video games need to constantly update your screen with new calculations, rendering, and special effects. In cases like these, a while-loop that runs for a very long time is necessary.

Nested Loops

Like the if-statements we looked at in the “Booleans” article, loops can be placed inside one another. A loop inside another loop is called a nested loop. For example, this code prints the numbers 0-2 three times.

for i in range(3):
    for j in range(3):
        print(j)
> 0
> 1
> 2
> 0
> 1
> 2
> 0
> 1
> 2

You can play with all the code we've used in this article on Trinket: