Loop Over Range List in Python (3 Examples)

In this tutorial, you’ll learn how to loop over a range of numbers in Python using the range() function. We’ll explore different examples to demonstrate this concept.

The table of contents is structured as follows:

Let’s dive into it!

Example 1: Looping Over a Range

The first example involves looping over a range of numbers using a for loop. Here’s how it works:

for num in range(5):
    print(num)
# 0
# 1
# 2
# 3
# 4

In this example, we use the range() function to generate a sequence of numbers from 0 to 4 (5 numbers in total). The for loop iterates over each number in the range, and we print the values of num.

Example 2: Looping Over a Specific Range

The second example demonstrates how to loop over a specific range of numbers using the range() function. Here’s an example:

for num in range(2, 8):
    print(num)
# 2
# 3
# 4
# 5
# 6
# 7

In this example, we specify the start and end values of the range in the range() function. The loop iterates over the numbers from 2 to 7 (excluding 8), and we print each number num.

Example 3: Looping Over a Range with a Step Size

The third example demonstrates how to loop over a range of numbers with a specific step size using the range() function. Here’s an example:

for num in range(1, 10, 2):
    print(num)
# 1
# 3
# 5
# 7
# 9

In this example, we specify the start, end, and step size values in the range() function. The loop iterates over the numbers from 1 to 9 (excluding 10) with a step size of 2, and we print each number num.

Video, Further Resources & Summary

Do you need more explanations on looping over a range list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

The YouTube video will be added soon.

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to loop over a range list in Python using the range() function. In case you have further questions, you may leave a comment below.

Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top