Range function in Python 3

by Alex
Range function in Python 3

The range type is a built-in data type in Python, which is designed to store information about an arithmetic progression of integers. To generate it, a range function with the same name and three different parameters is used. Range provides the convenience of working with collections when processing them in cycles.

What is a range?

A range is an immutable sequence of integers that is most often used to generate more complex data sets using generators. But unlike lists, tuples, and other standard collections, it always requires an equally small amount of memory to process such an object. Due to a special approach to data storage, not all elements of a sequence are written to memory, but only its length, step, and starting point. The rest of the data is calculated as needed.

Range Function

To generate a range, call the range function, passing it 1 to 3 integer arguments. In Python the range is an independent object. So you can create it by assigning the result of range to a certain identifier. You can use the type() method to check what class the object belongs to.

print(type(range(10)))

<class 'range'>

Let’s look at the description of the range function in Python 3. As mentioned above, it takes several parameters:

  1. Range start (start);
  2. Range end (stop);
  3. Range step.

It is not necessary to specify all of them, since start and step have 0 and 1 values, respectively, by default. However, it is still necessary to specify stop for the range.

If you specify only one argument, the end of the range (stop) is specified. If more than one argument is specified, the order is: start, stop, step.

The next example shows the result of the range function.

data = range(10)
print(data)

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

So, we got a sequence with 10 integer elements, because the values of start and step parameters were left by default. Let’s try to add one argument.

data = range(3, 10)
print(data)

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

As you can see, the first parameter of the range function sets the starting value of the future sequence (3), while the second parameter shows its final boundary (10). However, contrary to expectations, the number 10 is not included in the final sequence. Let’s add a third argument to the function and see what happens.

data = range(3, 10, 2)
print(data)

[3, 5, 7, 9]

This sets the range of numbers in Python 3 using as many parameters as possible, which are responsible for the beginning (3), the end (10), and the step (2). Thus, the function print outputs 4 integer values with the same sequence interval. It is always worth considering the order of its arguments: start, stop, and step. All of them can only have an integer value, either positive or negative. Ranges are often used to quickly generate a list or other collection of numbers. To do this, all you have to do is pass the result of the execution to list. The following example shows how to generate a list.

data = list(range(10, -25, -5))
print(data)

[10, 5, 0, -5, -10, -15, -20]

Reverse order output

Thanks to the implementation of the collections.abc.Sequence ABC interface, objects of the range class can be handled in different ways, like lists or tuples. Thus, ranges have the ability to check for occurrence, index search, slice, or negative indexing. The following example shows the output of a list in Python 3 c ranges in reverse order.

data = list(reversed(range(10, -35, -10))
print(data)

[-30, -20, -10, 0, 10]

This uses the built-in reversed function that processes a sequence of some data and returns each of its elements in the opposite order.

The difference between range and xrange

Sometimes when working with old code written in the days of Python 2, you may come across a method called xrange. This is very similar to the standard range. The only difference between xrange and range is that when you call the former, a new object of type range is created, not the usual list of class list. In this case, the elements of the sequence are not stored in memory, but are generated as the program runs to save resources.

In Python 3, the range function was removed entirely, and xrange was renamed to range.

This was most likely done to improve the efficiency of working with large sequences of numbers. For example, in older versions of the language, using a range function with a value higher than sys.maxsize would cause an error and cause the program to terminate.

Conclusion

So, the range type has a lot of uses, because it allows us to easily automate the process of filling in different sets of numbers. As a rule, for this purpose, the range function is used, where the initial element, the boundary, and the step of the sequence are specified. Due to the implementation of the collection interface, ranges provide occurrence checking, index search, slice, and negative indexing of elements.

Related Posts

LEAVE A COMMENT