How to make a range in Python

Learn how to make a range in Python with an example. Understand how to use range() to generate a list of numbers in a given range and how to loop through it.

Creating a Range in Python

In Python, the range() function is a convenient way to generate sequences of numbers. It allows us to generate sequences of numbers, which we can then use to perform certain operations on. The syntax of the range() function is as follows:

range(start, stop, step)

Where start is the starting number, stop is the ending number, and step is the step size. It is important to note that the stop parameter is not inclusive, so the range will not include the number specified by the stop parameter.

For example, if we wanted to generate a range of numbers from 0 to 10, we would use the following code:

for num in range(0, 11):
    print(num)

This would output the following:

0
1
2
3
4
5
6
7
8
9
10

We can also use the step parameter to specify a step size. For example, if we wanted to generate a range of numbers from 0 to 10 where each number was incremented by 2, we would use the following code:

for num in range(0, 11, 2):
    print(num)

This would output the following:

0
2
4
6
8
10

The range() function is a powerful and convenient tool for generating sequences of numbers in Python. It is simple to use and allows us to easily generate sequences of numbers that we can use for various operations.

Answers (0)