Search

Python Range() Function: The Complete Guide

Imagine you want to loop over some numbers that is defined by a sequence. What do you do? Start creating the list of numbers by hand? Nope, not in python. Python provides a convenient data type that does just that operation for you and that is the python range type that produces a sequence using the python range function.

python range function

 

What is the python range function?

The python range function represents a range type that produces an immutable sequence of numbers. The syntax of the python range function is of two types: range(stop) and range(start, stop[, step]). The first, with only the stop argument is used when you want to produce an immutable sequence of numbers that follow themselves sequentially while the second is used when you want to explicitly define the sequence with a starting integer and specify the steps to take.

The python range function produces a range object which is a sequence. Therefore, you can extract the elements by casting it to a list or using it in a for loop.

Let’s take the syntax with examples.

1. Range(stop) examples.

With the python range function specifying only the stop argument, you are asking the function to create an immutable sequence that starts from 0 and ends at the number just before the stop integer, creating integers consecutively. That is, the stop integer is not included in the range object that is created but acts as a boundary.

For example, run this code to see how it works.

You can see from the code that I first denoted the range object created by specifying the stop to be 20. Then I extracted the numbers in the range object by casting it to a list which prints out a sequence that begins from 0 to 19; remember the stop, here 20, is not included in the sequence that is created.

Note that the stop argument cannot be zero or no sequence will be created.

We can bring out the elements of the sequence using a python range for loop but that would be for the next examples on the second syntax.

2. range(start, stop[, step]) examples

This is the second way of using the python range function but using this you want to customize the sequence that is produced. The arguments to the function in this syntax are start, stop, and an optional step. All the arguments must be integers. The default for the start argument is 0 and the default for the step argument is 1. By changing the defaults, we can customizes the sequences we want to create.

For example if we have the arithmetic sequence: 1, 4, 7, 10, 16. How do you create this sequence using the range function? Simple. You can see that the sequence starts from 1. So in the range function, we denote start as 1. Also, you can see that the last item in the sequence is 16 and since stop is not included in the sequence but acts as a boundary, then in the range function we denote the stop as 17. Then we can see that the next item in the sequence from the earlier one has a difference of 3. So, we denote the step in our range function as 3. Therefore, the range function we will use is: range(1, 17, 3). This is how the code could be written:

When you run it, you can see that it reproduces just our arithmetic sequence.

Now there are some important points you need to note about this syntax using three arguments.

You can have both positive steps and negative steps. That means, as you can create a sequence going forwards, you can also create the sequence going backwards by just making the step a negative value. The example arithmetic sequence I gave above is a sequence that goes forwards. Now, we can make it go backwards by changing the start, stop, and step values.

Notice that the last item is 16 and going backwards we want to start from it. So, now our start will be 16. Since the first item above was 1 and going backwards it will be our last item. So, our stop will be just the boundary of 1 which is 0. Then our step is going backwards three steps, and therefore, -3. So, we would create our range function as: range(16, 0, -3). Run the code below to see it.

What I did was switch the start and stop to reflect the backwards movement and made step to be negative.

If you are confused, just notice that the function uses the formula: r[i] = start + step*i, where r[i] denotes each item in the sequence.

Ranges are sequences of integers

Yes, just as I said before, a python range object is just a sequence of integers. That means you can carry out sequence operations on range objects. If you want a refresher on what sequences are, see this post on iterables and sequences. But there are some sequence opertions that python ranges do not support.

First, let me outline some of the sequence operations python ranges support:

  1. You can do slicing on range objects.
  2. You can get the length of range objects.
  3. You can use them in for loops (as I showed above)
  4. You can use them in ‘in’ and ‘not in’ operations
  5. You can call the max and min functions on them.

Now, let us buttress the operations above with examples. For example, if we want to produce a python range object that denotes all the odd numbers from 1 to 10. Here is code that does it and that illustrates all the sequence operations I outlined above.

You can see that the operations I outlined above can be done with range objects.

But two sequence operations python range objects cannot carry out are concatenation and repetition. These two operations goes against the concept of ranging because they would create a new range out of the original range if permitted, thereby distorting the range itself.

Advantages of using a range object or the range function

While for small sequences you can use a list or tuple and hand code them, for large sequences, this might not be feasible, so using a range object with set start, stop and step parameters would be more adequate.

Also, the memory footprint of a range object is more efficient and smaller than that of lists or tuples. So, it would be more efficient to use a range object for sequences of numbers that you would have to call when needed.

That’s all folks. Now you have all you need to start using range objects. Use them to your heart’s delight.

Happy pythoning.

No comments:

Post a Comment

Your comments here!

Matched content