Python range: the complete range function guide

python range

The range function is a built-in function in Python that allows you to generate sequences of numbers in a simple and controlled way. Thus, the function is very useful in many cases, from a simple iteration over a list or dictionary to creating checklists and other more complex cases.

In this article, we’ll explore the functionality of the range function in Python, including its basic and advanced syntax, how to use it in a loop, and compare it to other solutions available in Python. Let’s start by introducing the basic syntax of the range function, as well as ways to change its parameters to generate custom sequences.

Next, we’ll show you how to use the range function in different types of loops, such as a for loop and a while loop, to perform operations on each number in the generated sequence. We will also show some practical examples of using the range function in Python programs.

Finally, let’s compare the range function with other solutions available in Python, such as the xrange function and the range() class. By the end of the article, we will have a deeper understanding of how to use the range function in Python and how to apply it to solve programming problems in different contexts.

Range function syntax in python

Basic syntax

The basic syntax of the range function in Python. Which allows you to create a sequence of values ​​or integers, with optional increment or difference between consecutive values.

Example to generate sequence of integers:

range(1, 10) # Generate the sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Alternatively, you can also specify the sequence of values ​​directly:

range(1, 9, 2) # Generate the sequence [1, 3, 5, 7, 9]

And you can also use the operator   *:

range(1, 9, 2)*2 # Generate the sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Advanced syntax

In addition to the basic syntax, there are several advanced features that make the function more powerful and versatile.

Consequently one of these features is the ability to use the argument stepto specify a custom increment or difference between consecutive values ​​in the string. Thus allowing to generate a sequence of data with a specific pattern, such as a sequence of prime numbers or a sequence of Fibonacci numbers.

See an example:

range(start, stop, step)

where:

  • start is the first value in the sequence you want to generate
  • stop is the last value in the sequence you want to generate
  • step is the increment or difference between consecutive values ​​in the sequence (optional)

You can use the function  range() to generate a sequence of values ​​and then use those values ​​to create a list or set. See in the example, the function  generate_sequence receives the three arguments:  start,  stop and  step. O  start is the start value of the sequence, o  stop is the end value of the sequence, and o  step is the step size between values ​​in the sequence. The function returns the generated sequence of integers:

# Function to generate a sequence of integers
def generate_sequence(start, stop, step):
    # Creates the sequence of integers
    sequence = range(start, stop, step)
    return sequence

# Example of using the function
sequence = generate_sequence(1, 10, 1)
print(sequence) 

The output will be:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Another way to use it is as a loop. In this example, the function  generate_sequence is used inside a loop while  to generate a sequence of integers from 1 to 10 and print each value in the sequence:

# Function to generate a sequence of integers
def generate_sequence(start, stop, step):
    # Creates the sequence of integers
    sequence = range(start, stop, step)
    return sequence

# Example of using the function as a loop
i = 1
while i <= 10:
    print(i)
    i += 1

The output will be:

1
2
3
4
5
6
7
8
9
10

Range function with map function

Another advanced feature of the range function is its ability to be used in conjunction with the  map(). The function map() lets you apply a specific function or operation to each value in the sequence, and the function range() lets you specify the range of values ​​over which the function should be applied. This allows you to create amazing tools.

In this example, the function  add_one is a function that adds 1 to each number passed to it. The function generates a sequence of integers from 1 to 10. Then the function applies the function to each element of the generated sequence and prints the resulting list. Look: generate_sequence map() add_one

# Function to add  to each number in the sequence
def add_one(number):
    return number + 1

# Function to generate a sequence of integers
def generate_sequence(start, stop):
    # Creates the sequence of integers
    sequence = range(start, stop)
    return sequence

# Example of using the function
sequence = generate_sequence(1, 10)
mapped = map(add_one, sequence)
print(mapped)

The output will be:

 [2, 3, 4, 5, 6, 7, 8, 9, 10]

Another example, the function  add_two is a function that multiplies each number passed to it by 2. The function  generate_sequence is used to generate a sequence of integers from 1 to 10. Then the function  map() is used to apply the function  add_two to each element of the sequence generated and the resulting list is printed. Look:

# Function to add 2 to each number in the sequence
def add_two(number):
    return number * 2

# Function to generate a sequence of integers
def generate_sequence(start, stop):
    # Creates the sequence of integers
    sequence = range(start, stop)
    return sequence

# Example of using the function
sequence = generate_sequence(1, 10)
mapped = map(add_two, sequence)
print(mapped)

The output will be:

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Overall, understanding its advanced features and various usages, you can use the function range() to generate valuable and useful data for your projects and to create more powerful and efficient data analysis and software development tools.

Generating sequences with the range function

Continuous Sequences

The Python function range() generates a sequence of continuous numbers. It takes three parameters: the first is the starting number of the sequence, the second is the last number of the sequence and the third is the step of the sequence.

When using the function  range() with a step of 1, you will get a continuous sequence of integers, starting with the starting number and ending with the last number. For example,  range(1, 6) it will generate the sequence of integers from 1 to 5.

range1 = range(1, 6)
print(range1)

The output will be:

range(1, 6)
[1, 2, 3, 4, 5]

If you want a partial sequence with a different step, you can specify the step as the third parameter of the function  range(). For example,  range(1, 10, 2) it will generate the partial sequence of integers from 1 to 8, with a step of 2.

range2 = range(1, 10, 2)
print(range2)

The output will be:

range(1, 10, 2)
[1, 3, 5, 7, 9]

Note that if you specify the last number as the function’s second parameter  range(), it will generate a continuous sequence of integers, starting with the starting number and ending with the last specified number. For example,  range(1, 11) it will generate the continuous sequence of integers from 1 to 10.

range3 = range(1, 11)
print(range3)

The output will be:

range(1, 11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

However, if you need to generate continuous sequences with custom values, you can change the function’s parameters  range(). For example,  range(10) it will generate a continuous sequence of integers from 0 to 9. If you want to start the sequence at the number 100, you can do this by setting the second parameter to 100:

range4 = range(100)
print(range4)

The output will be:

range(100)
[100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30,29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

This is one of the ways to generate continuous sequences with the range. You can also adjust the sequence step using the third parameter, as mentioned earlier. Remember that if the second parameter is less than the first, the function range() will generate an empty string.

Partial Sequences

The Python function range() can generate partial sequences, that is, sequences that contain a limited number of elements. Therefore, you can specify a second parameter greater than the first, which will cause the function to return an empty string.

For example,  range(5, 15) it will generate the partial sequence of integers from 5 to 14.

range5 = range(5, 15)
print(range5)

The output will be:

range(5, 15)
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

On the other hand, if you want a partial sequence with a different step, you can specify the step as the third parameter of the function  range(). In the example, it will generate the partial sequence of integers.

range(1, 10, 2)

The output will be:

[1, 3, 5, 7, 9]

Using the range function in a loop for 

The function  range is a built-in function in Python that allows you to generate sequences of numbers. We can use a for loop to iterate over a sequence of numbers in Python and perform an action for each number in the sequence.

That way, to use the function  range in a for loop , just pass the argument  range to the function  for and specify the necessary parameters. For example, to iterate over a sequence of numbers from 0 to 4, the code would be:

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

The result would be:

0
1
2
3
4

Also, you can use the function  range in a for loop to iterate over a sequence of numbers, with or without increments. For example:

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

Likewise, the result would be the same as the previous example.

You can also specify optional parameters  range to generate a sequence of different numbers. For example, to iterate over a sequence of numbers from 1 to 10, excluding even numbers, the code would be:

for i in range(1, 11, 2):
    print(i)

The result would be:

1
3
5
7
9

In short, the function  range creates a sequence of integers, starting at the number specified in the first parameter and ending at the number specified in the second parameter. Furthermore, if you need a sequence of non-integer numbers or with a different range, you can use other functions such as  xrange,  range(stop=None) or  range(1, stop), respectively.

Practical examples of using the function  range in Python programs

The function  range is a built-in function in Python that allows you to generate sequences of numbers. Python programs can use the language in many practical situations.

Creating a list containing the first 1000000000 (10 billion) perfect numbers.

As a result, a practical example of using the function  range would be to create a list containing the first 1000000000 (10 billion) perfect numbers. For this, it is possible to use a loop  for and the function  range as follows, in the code below it will create a called list  numbers that contains the first 10 billion perfect numbers. We will print each number to the screen with each iteration of the loop. Look:

n = 1000000000
numbers = []

for i in range(n):
    numbers.append(i)
    print(i)

Creating a list containing numbers from 1 to 100, added alternately.

In this way, it is possible to use a loop  for and the function  range as follows, in the code below it will create a called list  numbers that contains the numbers from 1 to 100, added alternately. So we’ll add each number to the list if it’s even, or subtract 1 if it’s odd. Look:

numbers = []

for i in range(1, 101):
    if i % 2 == 0:
        numbers.append(i)
    else:
        numbers.append(i - 1)

print(numbers)

The result will be a list with the numbers 1, 3, 5, 7, …, 97, 99.

Creating a list containing numbers from 1 to 1000, divided into groups of 10.

Soon we can see that it is possible to use the function  range in conjunction with a cycle  for to divide a sequence of numbers into groups. To do this, just specify the number of elements you want to include in each group, adding the parameter  step to the function  range.

For example, we can create a list that contains the numbers from 1 to 1000, divided into groups of 10, the code would be:

numbers = []

for i in range(1, 1001, 10):
    numbers.append(i)

print(numbers)

So if you specify the parameter  step as 1, the function  range will generate a sequence of continuous numbers, starting at the specified number and ending at the specified number. However, if you specify a  step negative, the sequence of numbers will be generated in reverse order. If you specify a  step different than 1, the sequence of numbers will be generated with a different interval than normal.

Comparison of range function in python with other available solutions

The function  rangein python is a built-in function that returns a sequence of contiguous integers. This function is equivalent to the function  xrange() in Python 2, which returns a sequence of contiguous integers, on the other hand, instead of returning a copy of the numbers, it returns an object  xrange that is a reference to a table of numbers.

In fact, class  rangein python is a class where derived from class  xrange(), and has the same methods and attributes. However we have the main difference between the two is that the class  range() is used to create an instance of an object  range, while the function  xrange() returns a reference to an object  xrange.

Although the class  range is a more modern way of accessing the sequence of contiguous integers, the function  xrange() is still widely used in older code and in some situations where you need to create a  xrange.

Still, in terms of performance, function  range is more efficient than class  range()because it is a built-in function and does not require creating a new object instance. Also, class  range() is more complex than function  xrange(), and may be less readable for developers.

In general, the choice between using the function  rangein python, the function  xrange() or the class  range() depends on the specific needs of the project and the experience of the developer.

Applications of the range function in python

People widely apply Python’s range() function across a variety of applications, including:

  1. Mathematical calculations:  People often use the function to generate sequences of numbers in mathematical calculations, such as iterating over a range of numbers.
  2. Testing Programs:  People widely use Python to generate sequences of values ​​in testing programs.
  3. Generating random numbers:  The function  range() can be used to generate sequences of random numbers, such as in games or simulations.
  4. Object-oriented programming:  The function  range() is often used in object-oriented programming to generate sequences of values ​​for testing programs.
  5. Image processing:   Developers can use Python to generate sequences of numbers and apply them in image processing, such as filtering algorithms, etc.

Conclusion

The  range Python function is a very useful and versatile built-in function for generating sequences of contiguous integers. Thus people widely use the Python language in various applications, which include mathematical calculations, program testing, random number generation, object-oriented programming, and image processing. Although the class  range() is a more modern way of accessing the sequence of contiguous integers, the function is still widely used and is a popular choice for many developers. Although the class  range is more complex than the function  xrange(), the function  range() is more efficient and more readable for developers. In short, the function range() is a valuable tool for anyone working with Python and is widely used in many projects. Also learn about Scan in python !

Was this helpful?

Thanks for your feedback!

Schenia T

Data scientist, passionate about technology tools and games. Undergraduate student in Statistics at UFPB. Her hobby is binge-watching series, enjoying good music working or cooking, going to the movies and learning new things!

Leave a Reply

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