2. Introduction
• Tuples are used to store multiple items in a single
variable.
• A tuple is a collection which is ordered and
unchangeable.
2
3. Why tuples?
• Suppose a method in your application depends on a
particular sequence of objects remaining the same
throughout the lifetime of the code.
• If you store these objects in a list, there is a chance
that some other method using the list will accidentally
alter it and thus break your method.
• Thus, in a way, a tuple offers a guarantee that a
particular collection of objects will remain fixed.
3
4. Tuples
• Tuples are immutable, so they can't be changed.
• As a result, you can't use any of the skills for adding, modifying,
or removing items.
• Example:
• If you have data that doesn't change, implementing it as tuple
will guarantee that it remains write-protected.
5. Tuples
• How to create a tuple
mytuple = (iteml, item2, . . .)
• Code that creates tuples
# a tuple of 5 floating-point numbers
stats = (48.0, 30.5, 20.2, 100.0, 48.0)
# a tuple of 6 strings
Ice_cream_flavors = (“Vennila”, “strawberry”,
“chocolate”, “pista”,”badam”)
5
6. Tuples
• Code that accesses items in a tuple
Ice_cream_flavors[0] # Vennila
Ice_cream_flavors[-1] # badam
Ice_cream_flavors[1:4] # (“strawberry”, “chocolate”,
“pista”)
• Code that unpacks a tuple
tuple_values = (1, 2, 3)
a, b, c = tuple_values # a = 1, b=2, c=3
6
8. Tuples
Tuples provide just two methods,
• t.count(x), which returns the number of times object x occurs in
tuple t,and
• t.index(x), which returns the index position of the leftmost
occurrence of object x in tuple t.
Tuples can be used with the operators
• + (concatenation),
• * (replication), and
• [] (slice), and
• with in and not in to test for membership.
• The += and *= augmented assignment operators can be used
even though tuples are immutable
9. 9
Method/Function Description
count()
Returns the number of occurrences of a
specified element in a tuple.
index()
Returns the index of the first occurrence of a
specified element in a tuple.
len() Returns the number of elements in a tuple.
sorted()
Returns a new tuple with the elements sorted
in ascending order.
min() Returns the smallest element in a tuple.
max() Returns the largest element in a tuple.
tuple() Converts an iterable object into a tuple.
10. Tuples - Example
Using tuple() method to create new tuple
• >>> t=tuple((1,2,3))
+ (concatenation operator usage in tuples)
• >>> t1=(4,5,6)
• >>> print(t+t1)
• (1, 2, 3, 4, 5, 6)
• >>> t3=t+t1
• >>> t3
• (1, 2, 3, 4, 5, 6)
• >>> t[:1] + (5,) + t[1:]
• (1, 5, 2, 3)
* (replication operator usage in tuples)
• >>> t*2
• (1, 2, 3, 1, 2, 3)
Finding length of a tuple
• len(t) returns 3
11. Tuples - Example
[] (slicing in tuples )
• >>> t[2]
• 3
+=, *= (augmented operator usage in tuples)
• >>> t2=()
• >>> t2+=t
• >>> t2
• (1, 2, 3)
• >>> t2*=2
• >>> t2
• (1, 2, 3, 1, 2, 3)
Count function in tuples:
• >>> t.count(1)
• 1
Index function in tuples:
• >>> t.index(1)
• 0
12. Nested Tuples
• In Python, a tuple can be defined inside another
tuple; called Nested tuple.
• In a nested tuple, each tuple is considered as an
element.
• The for loop will be useful to access all the elements
in a nested tuple.
• Toppers = (("Vinodini", 98.7), ("Soundarya",
97.5),("Tharani", 95.3), ("Saisri",93.8))
12
13. Nested Tuples - Example
Toppers = (("Vinodini", 98.7), ("Soundarya",
97.5),("Tharani", 95.3), ("Saisri",93.8))
for i in Toppers:
print(i)
13