# An Introduction to the Python Range Function.

#### Let’s learn about the python range function in detail. Photo by Drew Beamer on Unsplash

### Range:

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.

range(stop)
range(start,stop,step)

start The value of the start parameter (or 0 if the parameter was not supplied)

stop The value of the stop parameter

step The value of the step parameter (or 1 if the parameter was not supplied).
If the step is 0, it will raise ValueError.

The arguments to the range function should be integers. (either built-in int or any object that implements the __index__ special method)

Example 1:Only the stop parameter is given.

range(10)

• start by default will be 0 and step by default will be 1
• stop is given as 10.
• stop value is excluded. It generates value until 9 only.
• It will return a range object containing numbers starting from 0 to 9.
• We can convert the range object to list using list() constructor.
• We can also iterate using for loop
r=range(10)
print (r)#Output:range(0, 10)
print (type(r))#Output:<class 'range'>
print (list(r))
#Output:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 2:Only the start and stop parameter is given.

range(1,10)

• step by default will be 1
• It will generate a sequence of numbers starting from 1 to 9.
r=range(1,10)
print (r)#Output:range(1, 10)
#Converting range object to list
print (list(r))
#Output:[1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 3:start, stop and step parameter is given

range(1,10,2)

• It will generate a sequence from 1, increment by 2, and will stop at 9.
r=range(1,10,2)
print (r)#Output:range(1, 10, 2)
#Converting range object to list
print (list(r))
#Output:[1, 3, 5, 7, 9]

Example 4:We can also decrement step by mentioning a negative number.

range(10,1,-2)

• It will generate a sequence of numbers from 10, decrement by 2, and stop at 1.
• Iterating through range object using for loop.
r=range(10,1,-2)
print (r)#Output:range(10, 1, -2)
for i in r:
print (i)
'''Output
10
8
6
4
2
'''

Example 5:

r=range(0)
print (r)#Output:range(0,0)
print (list(r))#Output:[]

r1=range(2,2)
print (list(r1))#Output:[]

Example 6: step is given as 0. It will raise ValueError.

r=range(1,10,0)
print (r)
#Output:ValueError: range() arg 3 must not be zero

Example 7: start, stop, and step can be negative numbers also.

r=range(-10,-20,-2)
print (list(r))
#Output:[-10, -12, -14, -16, -18]

Example 8: start, stop, and step is given as variables a,b,c.

a=1
b=5
c=2
r=range(a,b,c)
print (list(r))
#Output:[1, 3]

Example 9: range() function doesn’t support float numbers.It will raise TypeError.

r=range(2.0,10.0,2)
#Output:TypeError: 'float' object cannot be interpreted as an integer

#### Common Sequence Operations on range object:

1. Membership test
2. Indexing
3. Slicing
4. len()
5. min()
6. max()
7. index()

#### 1. Membership Test

Membership test can be done by using in and not in operator.

x in s-True if an item of s is equal to x, else False

x not in s-False if an item of s is equal to x, else True

Example:

a1=range(5)
print (3 in a1)#Output:True
print (5 not in a1)#Output:True

#### 2.Indexing

Indexing also supported in range objects.

Indexing starts from 0. Index 0 represents the first element in the sequence.

Negative indexing starts from -1. Index -1 represents the last element in the sequence.

r=range(2,10,2)
print (r)#Output:2
print (r)#Output:8
print (r[-1])#Output:8

### IndexError

Attempting to use an index that is too large will result in an IndexError.

r=range(2,10,2)
print (r)
#Output:IndexError: range object index out of range

### 3. Slicing:

Slicing is supported by the range object.

Refer to my story of Indexing and Slicing.

In slicing, we can specify a range of indexes.

s[i:j:k] — slice of s from i to j with step k

Example: s[1:3] — Returns element from the first index to the third index(excluded).

r=range(2,10,2)
print (list(r[1:3]))
#Output:[4, 6]

#### 4.len()

Returns the number of elements in the range object.

Example:

r=range(2,10,2)
print (len(r))
#Output:4

#### 5.min()

Returns the smallest element in the range object.

r=range(2,10,2)
print (min(r))
#Output:2

r1=range(-10,-20,-2)
print(min(r1))#Output:-18

#### 6.max()

Returns the largest element in the range object.

r=range(2,10,2)
print (max(r))
#Output:8

r1=range(-10,-20,-2)
print(max(r1))#Output:-10

#### 7.index()

Returns the index of the specified element in the range object. If the element is not in the range object means, it will raise a ValueError.

r=range(2,10,2)
print (r.index(4))
#Output:1
print (r.index(10))
#Output:ValueError: 10 is not in range

#### Concatenation and repetition operator:

The concatenation and repetition operator is not supported in the range object.

Range object only supports item sequences that follow specific patterns, and hence don’t support sequence concatenation or repetition.

• Concatenation is not supported in range objects.
r1=range(5)
r2=range(10)
print (r1+r2)
#Output:TypeError: unsupported operand type(s) for +: 'range' and 'range'
• The repetition operator is not supported in the range object.
r1=range(5)
print (r1*2)
#Output:TypeError: unsupported operand type(s) for *: 'range' and 'int'

#### Concatenation by using itertools.chain()

Python doesn’t have a built-in function to concatenate two or more range objects. We can achieve this by using itertools.chain().

chain():
Makes an iterator that returns an element from the first iterable until its exhausted, then proceeds to the next iterable. It will treat consecutive sequences as a single sequence.
itertools.chain(*iterables)

Example:

import itertools
r=range(2,10,2)
r1=range(-10,-20,-2)
r2=itertools.chain(r,r1)
print (list(r2))
#Output:[2, 4, 6, 8, -10, -12, -14, -16, -18]

#### Advantage of range function:

The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start, stop and step values).

#### Conclusion:

1. The range function supports only integers. (either built-in int or any object that implements the __index__ special method)
2. If the step is 0, it will raise ValueError.
3. The range function doesn’t support concatenation and repetition.
4. Python doesn’t have a built-in function to concatenate two or more range objects. We can achieve this by using itertools.chain().

#### Resources(Python Documentation):

Common Sequence Operations

https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range

range

https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range

range function

https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range

One-Time
Monthly
Yearly

#### Make a yearly donation

Choose an amount

$5.00$15.00
$100.00$5.00
$15.00$100.00
$5.00$15.00
$100.00 Or enter a custom amount$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.