Tuple Data Type and Methods in Python
In this class, we discuss Tuple data type and methods in python.
For Complete YouTube Video: Click Here
Table of Contents
Tuple Data Type
Before moving on to the tuple data type, The reader should understand the list data type. Click here.
Good to understand how memory is allocated to list data type. Click here.
The Tuple data type is the same as the list, whereas the list is changeable.
Tuple: Collection of compound data types. It allows duplicates. It is ordered and Unchangeable.
Unchangeable means are immutable. The discussion about mutable and immutable objects are explained previously. Click here.
Whenever you don’t want to change your data once defined, we use a tuple. Otherwise, use a list.
An example of when to use a tuple is shown below.
Days_in_a_week = (“mon”,”tue”,”wed”,”thur”,”fri”,”sat”,”sun”).
Define and Access Tuple
Let’s take examples and understand tuple data type.
t=(1,2,6) we define tuple using ().
print(t) will display (1,2,6)
Accessing elements in a tuple
t=(1,2,6)
print(t)
print(t[0])
We access elements in the tuple using index values.
As we already discussed, indexing in our string and list data types class. We are not explaining them again.
Negative indexes are also available on tuples.
Tuple within a tuple.
Example:
t=(1,2,6,(8,9),[5,9])
Third index position we defined tuple. And in the fourth index position, we defined the list.
A tuple is a collection of compound data types. We can mention the list in the tuple.
print(t[3][0]) will display 8. In the third position, a tuple is present. In the tuple, zero position number 8 is present.
print(t[4][1]) will display 9.
Define single value tuple
t=(“suresh”,) symbol, is mandatory to mention in single value tuple.
we can do it like t1=”suresh”,
print(t) will display (“suresh”,)
print(t1) will display (“Suresh”,)
Tuple function
The tuple is the function name used for type conversion.
In our type conversion class we discussed some of the functions used for type conversion. Tuple is one of it.
Example:
l=[1,2,6]
t=tuple(l) we are converting list into tuple.
print(t) will display (1,2,3)
Slicing in tuple
Example:
t=(10,20,30,40,50,60)
print(t[2:5]) will display (30,40,50)
print(t[::2]) will display (10,30,50)
We are not discussing slicing again. Assuming reader watched our list and string classes.
Operators
+ operator for concatenation.
Example:
a=(10,20)
b=(30,40)
c=a+b
print(c) will display (10,20,30,40)
+ used to concatenate two tuples
operator * for repetition
Example:
a=(10,20)
b=a*2
print(b) will display (10,20,10,20)
Methods and Functions
Examples:
a=(1,2,1,1,5)
print(len(a)) will display 5. len function will give number of elements in the tuple.
print(min(a)) will display 1. min function display minimum element in the tuple.
print(max(a)) will display 5. max function will display maximum element in the tuple.
print(a.count(1)) will display 3. count method will count the number of occurrences of the element given in count method.
In our example, we have given element 1.
print(a.index(1)) will display 0. index method will display the first index position. Where the element is present.
Immutability Exception Case
As we said tuple is an immutable object. But there is an exception in tuple.
Let’s take an example and understand the exception situation.
t = (1, [1, 2, 6])
y=t
print(id(t))
print(id(y))
print(t)
print(y)
In the above example, we defined a tuple. Inside the tuple, we had an integer and a list.
Variable y is given reference of tuple variable t.
We displayed the reference of the t and y variable.
Both were showing the exact memory location.
3055680332, 3055680332
Now we modify the list in the tuple.
t[1].append(7)
after modification, variable t and y should locate in a different location. Because tuple is immutable.
But here, it is showing the same memory location
print(id(t)) will display 3055680332.
print(id(y)) will display 3055680332.
The point to understand we are not changing the tuple. We are changing the list inside the tuple.