Loading...
Python

range Data Type

range Data Type represents a sequence of number.
The elements present in range Data Type cannot be modified i.e., range Data Type is immutable.

Form-1: range(10)
this form generates numbers from 0 to 9.

Eg:
r=range(10)
for i in r : print(i)  0 to 9

Form-2: range(10,20)

This generates numbers form 10 to 20

Eg:
r=range(10,20)
for i in r : print(i)  10 to 19

Form-3: range(10,20,2)
In this 2 means increment value

Eg:
r=range(10,20,2)
for i in r : print(i)  10,12,14,16,18

We can access elements present in the range Data Type by using index.
r=range(10,20)
r[0]==>10
r[15]==> IndexError: range object index out of range

We cannot modify the values of range data type.

Eg:
r[0]=100
TypeError: ‘range’ object does not support item assignment

We can create a list of values with range data type

Eg:
>>>l=list(range(10))
>>>l
[0,1,2,3,4,5,6,7,8,9]

Leave a Reply

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