Learn Core Python

Python range datatype

range datatype:-

è range datatype represents sequence of number.

è The elements presented in range datatype are not modifiable.

è range datatype is a immutable datatype.

 

different forms of range datatype:-

form1:- range(10)

It will generate values from 0 to 9 i.e 10 values.

E.g:-

r1 = range(10)
print(r1)
for v in r1:
    print(v)
print(type(r1))

O/P:-

range(0, 10)

0

1

2

3

4

5

6

7

8

9

<class 'range'>

 

form2:-range(start_value,end_value)

è It will generate values between start and end.

è start_value is included and end_value is excluded.

E.g:-

r1 = range(1,10)
print(r1)
for v in r1:
    print(v)
print(type(r1))

O/P:-

range(1, 10)

1

2

3

4

5

6

7

8

9

<class 'range'>

 

form3:- range(start_value,end_value,step_value or increment_value)

E.g:-

r1 = range(1,10,2)
print(r1)
for v in r1:
    print(v)
print(type(r1))

O/P:-

range(1, 10, 2)

1

3

5

7

9

<class 'range'>

 

à We can access elements of range datatype using index.

E.g:-

r1 = range(10)
print(r1)
print(r1[0])
print(r1[5])

O/P:-

range(0, 10)

0

5

 

 

à We cannot modify range datatype values.

E.g:-

r1 = range(10)
r1[5] = 100
print(r1)
print(r1[5])

O/P:-

TypeError: 'range' object does not support item assignment

 

None Datatype:-

è None means nothing.

è In other languages like C,C++, Java they use 'void' keyword.

è If you want to remove reference to variable then we will assign None.

E.g:-

a = None
print(a)
print(type(a))

O/P:-

None

<class 'NoneType'>

 

E.g2:-

x = 10
#
#
#
x = None
print(x)
print(type(x))