7. Python 'Range' Data Type


Range Data type:

 Range data type means a range of numbers or a sequence of numbers if we want to our application zero to hundred (10 or 100)  or 100 to 10000 and if want to represent a range of numbers then we go for the range data type.

How to create the range the data type.

Example  x = range(10),it’s equal to 0 to 9 only. We can check using type () function

Ex:  x =     x = range(10)

print(x)

print(type(x))

range(0, 10)   # Result

<class 'range'>

The range is python in build data type.

R = range (10) but It represents 0 to 9 numbers. or if we want to represent 0 to 7 then we take like this range (8).

x = range(10)

print(type(x))

<class 'range'>  # Result

Now we want to print all number in range object

Print(r) = range (0, 10)

But if we want to print all numbers then we go to loop concept (for loop, while loop)

But these things we discuss latter. But now testing purpose

r = range (10)

for x in r:

            print(x)

the result looks like this: 1,2,3,4,5,6,7,8,9 virtually.

This is the range datatype.

Now how to create range object and what are various options are available.

option1:

            range (n)

            n = (from 0 to n-1)

            r = range (10) # 0 to 9

            r = range (100) #0 to 99

option2:

            range (begin, end)

            n = (begin 0 to end -1)

            r = range (1, 10) # 0 to 9

            result looks like this: 1,2,3,4,5,6,7,8,9 virtually.

We absorb that in option1 we passed on an object in range and option2 we passed two objects and option3 also we passed three objects passed.

Option3:

            range (begin, end, increase or decrease value)

range (1,21,1) # it’s takes 1 to 20 with continue number

Results would be like this: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 virtually.

range (1,21,2) # it takes 1 to 20 with one after two number

Results would be like this: 1,3,5, 7, 9, 11, 13, 15, 17, 19 virtually.

range (1,21,3) # it takes 1 to 20 with one after three number

Results would be like this: 1,4,7, 10, 9, 13, 13, 16, 19 virtually.

range (21,1,-5) # it takes 1 to 20 with one after three number

Results would be like this: 20, 16, 12, 8, 4 virtually.

Range data type means a range of numbers or a sequence of numbers, if number sequence is there it’s apply to the indexing and slicing also.

Ex:

r = range (1, 21) # its takes 1 to 20 with continue number

print (r[0])

print (r[-1])

Results would be like this: 1, 20 virtually.

r = range (1, 21) # its takes 1 to 20 with continue number

r1 = r[2:5]

for x in r1:

    print (x)

Results would be like this: 3, 4, 5 virtually.

 Range is always represent a sequence of numbers

r = range (1, 21) # its takes 1 to 20 with continue number

            r[0] =1000 # it’s not possible.

Once we create the range object we can’t modify the inner numbers. That’s why the range data type is immutable.

IMP points:

1.      Range datatype means a range of numbers or a sequence of numbers.

2.     Range data type has various options

            Option1 = range (10)

Option2 = range(1,20)

Option3 = range(1,20,2)

3.     Order is there, so it’s take indexing and slicing.

4.     It’s immutable.

 

 

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post