In the previous tutorials, we have discussed the data types like int, float, str, list, bytes and bytearray. Today we will study the two more data types that remain i.e. tuple and range data types. As we already know that some data types can contain as a collection of elements in Python like set, list, etc. In a similar way the tuple and range data types also store a collection of elements. The tuple and range data types are very frequntly used in Python so let’s discuss more about them.
Tuple:
To much extent, the tuple data type is similar to list data type.
- The order of insertion remains preserved.
- Duplicates are allowed.
- Tuple can contain elements of various data types i.e. it is heterogeneous.
- But the only difference it has from a list is that it is immutable.
- It is declared within ‘( )’ parenthesis.
Example:>>> tup= (1, 2, 3, True, ‘Mew’)
Output:(1, 2, 3, True, ‘Mew’)
Here in the code above, we declare a variable named ‘tup’ of type tuple. It contains int elements like 1, 2, 3 and bool element ‘True’ along with a string literal ‘Mew’. This shows that it is heterogeneous.
- We can access the various elements of the tuple by using index numbers and slice operator can also be applied.
Example:
#variable tup is created >>> tup= (1, 2, 3, True, ‘Mew’) >>>tup[0] #element at index 0 is accessed >>>tup[1:4] #slicing operator is used on tup
Output:
#output from index 1 #output from slicing 2,3, True
Taking the variable ‘tup’ declared in the previous example we print its element at index 0 by using [0]. Then we use the slice operator to only obtain elements from index numbers 1 to (4-1) i.e. 3.
- For repeating the values upto n number of times we can make use of the ‘*’ operator followed by the list name.
Example:-
#variable declared 'tup' >>> tup= (1, 2, ‘Meow’, True) >>> tup1= tup*2 #tup1 will now contain duplicated value of tup >>> tup1
Output:(1, 2, ‘Meow’, True, 1, 2, ‘Meow’, True)
In the code above we take a variable named ‘tup’ of type tuple with elements 1, 2, ‘Meow’ and True. We then use the ‘*’ operand followed by 2 to get a duplicate value of the variable ‘tup’.
- We can have a list object inside a tuple.
Example:>>>tup= (1, 2, [1, 2])
Output:(1, 2, [1, 2])
In the code above we have three elements i.e. 1, 2 and [1, 2] where the third object is a list.
Range:
The range data type can contain a sequence of values and is very commonly used in Python. It is also immutable in nature.
- Form 1: range(end) – It represents values from 0 to 9 (end-1).
Example:
#the variable ran will contain 10 values >>> ran= range(10) #we use the type function to get its type >>> type(ran)
Output:<class ‘range’>
- For printing the entire elements of the range we can make use of the for loop.
Example:
>>>range(0, 10) >>>for i in ran: print(i) #using for loop to print the range
Output:
0 1 2 3 4 5 6 7 8 9
Note : We can access the various elements of the list by using index numbers and slicing is also applicable using slice operator.
Example:
>>>ran[2] #accesing element at index 2 >>>ran[0:3] #using slice operator on variable
Output:
#output from index 2 #output from slicing range(0, 3)
* Being immutable we cannot change the values once assigned.
Example:
>>>ran=range(10) #we try to assign a value at index 0 >>>ran[0]= 123
Output:TypeError: ‘range’ object does not support item assignment
- Form 2: range(10, 30) – It is used to represent values from 10 to 29.
- Form 3: range(10, 50, increment) – It is used to represent values from 10 to 49. Here we also include the number of steps that we want to be incremented along with the range.
Example:>>>for i in range(10, 50, 5) :print (i)
Output:
10 15 20 25 30 35 40 45
Note : However, float values cannot be used.
Example:>>>range(10.2, 40.3)
Output:TypeError: ‘float’ object cannot be interpreted as an integer
This was all about tuple and range data types in Python with some examples on how to use them with other fundamental data types like int, float and string literals.