Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
The for loop
3. for loop with range() – explanation
The while loop
Nested loops
break and continue


Good. Let's analyze what happened:

for i in range(1, 11):
  print('Current value:', i)

The program above printed ten lines of text, each time with a different number:

Current value: 1
Current value: 2
Current value: 3

We start our loop with the for keyword. After a space, we provide a name of our choice for a temporary variable – i, in this case. Next, in range(1, 11) means that i will be incrementally assigned values from 1 (inclusive) to 11 (exclusive) in subsequent loop iterations. Finally, we add a colon (:) to denote that a block should follow.

Next, we provide a block of code. It may be one or more instructions with equal indentation, just like in the case of if statements. These instructions will constitute the body of the loop.

The body is executed for each iteration. In the first iteration, i == 1, so Python will print Current value: 1. After this iteration, i will be incremented to 2, so Python will print Current value: 2. Python will continue for another eight iterations, until it prints Current value: 10. Note that range(1, 11) means that the loop will end before i == 11.


Modify the template code so that it prints numbers from 2 to 15.

Stuck? Here's a hint!

Remember that range(2, 15) will only print numbers from 2 to 14, without the number 15.