2.09K viewspython
0
0 Comments

the range() function in Python

momo Changed status to publish January 6, 2022
0
Frank (anonymous) 0 Comments

Intuitively, range makes a lot of sense. It’s used as follows: range([start], stop[, step]).

  • start is optional. When not provided, it defaults to 0. It is the starting number of the sequence.
  • stop is mandatory. range will generate numbers up to but not including the value of stop.
  • step is optional. When not provided, it defaults to 1. It is the difference between two elements in the sequence. It cannot be 0; in this case, a ValueError would be raised.

You can build some intuition when experimenting with it:

  • >>> list(range(10))
  • [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  • >>> list(range(0, 10))
  • [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  • >>> list(range(0, 10, 2))
  • [0, 2, 4, 6, 8]
  • >>> list(range(10, 0, -2))
  • [10, 8, 6, 4, 2]
  • >>> list(range(10, -1, -2))
  • [10, 8, 6, 4, 2, 0]

range cannot be used with floats.

In Python 3, the range function return a range object:

  • >>> type(range(10))
  • <class ‘range’>

range is an immutable sequence type. Immutable means that it cannot be ‘changed’ or ‘mutated’ after its creation. Sequence means a finite, ordered set that is indexed. You are without a doubt familiar with some immutable sequence types in Python. Strings and tuples are some good examples you are probably familiar with. An example of a mutable sequence type is a list. You can add or remove items after it’s created.

Sequences support the slice syntax, but only mutable sequences support item assignment. For example:

  • >>> my_string = “hello”
  • >>> my_list = [1, 2, 3, 4, 5]
  • >>> my_string[0] # 0 is the index here
  • ‘h’
  • >>> my_list[0]
  • 1
  • >>> my_string[0:2] # notice the similarity between slice syntax and the range function
  • ‘he’
  • >>> my_list[0:2]
  • [1, 2]
  • >>> my_list[::2]
  • [1, 3, 5]
  • >>> my_list.insert(0, 0) # mutating the list by inserting an element
  • >>> my_list
  • [0, 1, 2, 3, 4, 5]
  • >>> my_list[5] = 10 # mutating the list by changing an element
  • >>> my_string[1] = ‘a’ # attempting to mutate the string
  • Traceback (most recent call last):
  • File “<stdin>”, line 1, in <module>
  • TypeError: ‘str’ object does not support item assignment

I hope the above makes sense. I was able to change the list because it’s a mutable sequence type, but when I attempted to mutate the string, Python protested and told me I couldn’t do that. range is just like the string.

Similarly, I can’t mutate a range object, but I can use the slicing syntax, perform a containment tests, and check the index of an element:

  • >>> range(0, 11)[2]
  • 2
  • >>> range(0, 11)[::2]
  • range(0, 11, 2)
  • >>> 10 in range(0, 11)
  • True
  • >>> 10 in range(0, 10)
  • False
  • >>> range(0, 11).index(10)
  • 10
  • >>> range(0, 11)[2] = 3
  • Traceback (most recent call last):
  • File “<stdin>”, line 1, in <module>
  • TypeError: ‘range’ object does not support item assignment

It is important to note, and this is a very common misconception, that range is not an iterator. It is an iterable, which means that you can turn it into an iterator by using iter().

However, despite it not being a iterator:

  • >>> import sys
  • >>> sys.getsizeof(range(0, 11))
  • 48
  • >>> sys.getsizeof(range(0, 100))
  • 48
  • >>> sys.getsizeof(range(0, 1000))
  • 48
  • >>> sys.getsizeof(list(range(0, 11))
  • … )
  • 208
  • >>> sys.getsizeof(list(range(0, 100)))
  • 1008
  • >>> sys.getsizeof(list(range(0, 1000)))
  • 9112

In terms of memory consumption, it is similar to that of an iterator. We can see that the size of the range object does not increase as the number of the elements in the sequence increase, whereas the size of the list object does increase in the same situation. This is a major advantage is terms of memory consumption. Only the startstop, and step values are stored, while sequence values are calculated as required.

momo Changed status to publish January 6, 2022
You are viewing 1 out of 1 answers, click here to view all answers.
Write your answer.
Back to Top

Forgot password?

Enter your account data and we will send you a link to reset your password.

Your password reset link appears to be invalid or expired.

Log in

Privacy Policy

To use social login you have to agree with the storage and handling of your data by this website. %privacy_policy%

Add to Collection

No Collections

Here you'll find all collections you've created before.