Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Range Loops

check

Iterating over a sequence of numbers with ranges.


Loops Over a Range of Numbers

In Python, the for keyword can be used to create a loop over an existing sequence of values. The range(...) function can be used to generate an increasing or decreasing sequence of numbers for the for keyword to iterate over. Any indented code following the for line will be part of the loop.

Calling range(...) with a single number will start from 0, step up by 1 each time, then stop right before the specified number:

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

> 0
> 1
> 2
> 3
> 4

Calling range(...) with a pair of numbers will start at the first number, step up by 1 each time, then stop right before the second number:

for number in range(3, 6):
  print(number)

> 3
> 4
> 5

for number in range(12.7, 17.3):
  print(number)

> 12.7
> 13.7
> 14.7
> 15.7
> 16.7

Calling range(...) with three numbers will start at the first number, step up by the third number each time, then stop right before the second number:

for number in range(24, 64, 10):
  print(number)

> 24
> 34
> 44
> 54

for number in range(5, 0, -1):
  print(number)

> 5
> 4
> 3
> 2
> 1

Note: As shown in the second example above, if the third number is negative, the range will go down rather than up.

Practice: Counting Even Numbers

Using a loop and the range() function, print all even numbers from 0 to 20.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

Remember that you can provide a third argument to the range() function to specify the step size.

> 0
> 2
> 4
> 6
> 8
> 10
> 12
> 14
> 16
> 18
> 20
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
for number in range(0, 21, 2):
 print(number)

Practice: Reverse Counting

Print numbers from 10 to 1 in reverse order.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

A negative step can be provided as a third argument to the range() function to decrement the values.

> 10
> 9
> 8
> 7
> 6
> 5
> 4
> 3
> 2
> 1
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
for number in range(10, 0, -1):
  print(number)

Practice: Sum of Numbers

Write a loop that calculates the sum of all numbers from 1 to 100, and then print the sum.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

Create a variable before the loop to hold the sum. Inside the loop, add each number to the sum.

> 5050
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
sum = 0
for number in range(1, 101):
  sum += number
  print(sum)

If you can complete these challenges and understand how the loops and range() function work, you are making great progress! Keep it up!