Tuple
Tuple
Example:
tup1=("delhi","mumbai","pune","goa")
OR
tup1="delhi","mumbai","pune","goa"
OR
tup1=('delhi','mumbai','pune','goa')
OR
tup1='delhi','mumbai','pune','goa'
print(tup1)
Output:
('delhi', 'mumbai', 'pune', 'goa')
List Vs Tuple:
List Tuple
List provides many in-built methods. Tuples have less in-built methods.
List operations are more error prone. Tuples operations are safe
Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.
Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.
Ordered
When we say that tuples are ordered, it means that the items have a defined
order, and that order will not change.
Example:
tup1="delhi","mumbai","pune","goa"
print(tup1[0])
print(tup1[2])
Output:
delhi
pune
Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove
items after the tuple has been created.
Example:
tup1="delhi","mumbai","pune","goa"
print(tup1[1])="Punjab"
Output:
Error
Allow Duplicates
Since tuples are indexed, they can have items with the same value.
Example:
tup1="delhi","delhi","pune","goa"
print(tup1)
Output:
('delhi', 'delhi', 'pune', 'goa')
Tuple Length
To determine how many items a tuple has, use the len() function:
Example:
tup1=("delhi","delhi","pune","goa")
print(len(tup1))
Output:
4
Example:
tup1=("delhi")
print(type(tup1))
tup1=("delhi",)
print(type(tup1))
Output:
<class 'str'>
<class 'tuple'>
tup1=(["delhi","delhi","pune","goa"],"a",1)
print(tup1)
Output:
(['del 'delhi', 'pune', 'goa'], 'a', 1)
Negative Indexing
Negative indexing means start from the end.
-1 refers to the last item, -2 refers to the second last item etc.
Example 1:
tup1=(("delhi","delhi","pune","goa"))
print(tup1[-1])
print(tup1[-2])
print(tup1[-3])
print(tup1[-4])
Output:
goa
pune
delhi
delhi
Example 2:
tup1=("delhi","mumbai","pune","goa","punjab","hydrabad")
print(tup1[0:6])
print(tup1[:4])
print(tup1[2:])
print(tup1[0:8])
print(tup1[0:-4])
print(tup1[:-3])
print(tup1[-3:])
print(tup1[-4:-1])
Output:
('delhi', 'mumbai', 'pune', 'goa', 'punjab', 'hydrabad')
('delhi', 'mumbai', 'pune', 'goa')
('pune', 'goa', 'punjab', 'hydrabad')
('delhi', 'mumbai', 'pune', 'goa', 'punjab', 'hydrabad')
('delhi', 'mumbai')
('delhi', 'mumbai', 'pune')
('goa', 'punjab', 'hydrabad')
('pune', 'goa', 'punjab')
Example:
tup1=("delhi","mumbai","pune","goa","hydrabad")
tup2=("Jaipur",)
tup3=tup1+tup2
print(tup3)
Output:
('delhi', 'mumbai', 'pune', 'goa', 'hydrabad', 'Jaipur')
Deleting a tuple
Del method is used to delete the tuple completely.
Example:
tup1=("delhi","mumbai","pune","goa","hydrabad")
print(tup1)
del tup1
print(tup1)
Output:
('delhi', 'mumbai', 'pune', 'goa', 'hydrabad')
Example:
tup1=("a","b","c")
tup1.pop() #OR tup1.remove()
print(tup1)
Output:
Error
Note: Pop and remove method does not exist for tuple
Unpacking a Tuple
When we create a tuple, we normally assign values (index) to all the
elements it. This is called "packing" a tuple.
But, in Python, we are also allowed to extract the values back into variables.
This is called "unpacking".
Example:
tup1=("delhi","mumbai","pune","goa","hydrabad")
(a,b,c,d,e)=tup1
print(a)
print(b)
print(c)
print(d)
print(e)
Output:
delhi
mumbai
pune
goa
hydrabad
Note: While unpacking, number of variables used should be equal to number
of elements present in tuple. Othrwise Error will come.
Printing the elements using loop
We can print the elements of tuple using loops.
Example 1:
tup1=("delhi","mumbai","pune","goa","hydrabad")
for a in tup1:
print(a)
Output:
delhi
mumbai
pune
goa
hydrabad
Example 2:
tup1=("delhi","mumbai","pune","goa","hydrabad")
for a in range(len(tup1)):
print(tup1[a])
Output:
delhi
mumbai
pune
goa
hydrabad
Example 3:
tup1=("delhi","mumbai","pune","goa","hydrabad")
a=0
while a<len(tup1):
print(tup1[a])
a=a+1
Output:
delhi
mumbai
pune
goa
hydrabad
Multiply Tuples
If you want to multiply the content of a tuple a given number of times, you
can use the * operator.
Example:
tup1=("delhi","mumbai","pune","goa","hydrabad")
newtup1=tup1*2
print(newtup1)
Output:
('delhi', 'mumbai', 'pune', 'goa', 'hydrabad', 'delhi', 'mumbai', 'pune', 'goa',
'hydrabad')
Tuple Methods
Python has two built-in methods that you can use on tuples.
Method Description
index() Searches the tuple for a specified value and returns the position
of where it was found
Count() method
This method is used to count number of occurrences of an element in a
tuple.
Example:
tup1=("pune","mumbai","pune","goa","pune")
print(tup1.count("pune"))
Output:
3
index() method
This method is used to find index number of any particular element of the
tuple.
Example:
tup1=("delhi","mumbai","pune","goa","jaipur")
print(tup1.index("mumbai"))
Output:
1
Finding maximum and minimum element from tuple
We can find maximum and minimum element in numric as ell as text data
Example:
tup1=(34,67,43,12,58,93,8)
print(max(tup1))
print(min(tup1))
Output:
93
8
Example:
tup1=("delhi","mumbai","pune","goa","hydrabad")
print(min(tup1))
print(max(tup1))
Output:
delhi
pune