Lecture 34 – Python Tuples-II
Programming Fundamentals
Previous Lecture Review
• Introduction to Tuples
• Creating a Tuple
• Tuple Assignment
• Accessing Elements using Indexing and Slicing
• Negative Indexing
• Iterating over the elements in a Tuple
• Updating Tuple Elements
• Memory Concept
• Deleting Tuple Elements
Outline
• Tuple Operations
• Built-in Tuple Functions
• Finding the length of a tuple in Python
• Finding the Maximum and Minimum Value in a Tuple
• divmod() Function
• Tuple Methods
• count() Method
• index() Method
• Nested Tuples
• Accessing Nested Tuple Elements
• Example Program
Tuple Operations
• The various operations that we can perform on tuples are very similar to lists.
Python Expression Results Description
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation
('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') Repetition
3 in (1, 2, 3) True Membership
Built-in Tuple Functions
• Below is the list of some built-in functions that you can use with python tuples.
Sr.No. Function with Description
1 cmp(tup1, tup2): Compares elements of both tuple.
2 len(tup): Gives the total length of the tuple.
3 max(tup): Returns item from the tuple with max value.
4 min(tup): Returns item from the tuple with min value.
5 list(seq): Converts a tuple into list.
6 divmod(a,b): Returns a pair of numbers consisting of their quotient and
remainder.
Finding the length of a tuple in
Python
• The Python provides a built-in function len( ) to find the length of a tuple. Here,
the length of a tuple is the total number of elements in that tuple.
• Example:
>> student_data = (1, 'Rama', '2nd Year', 'CSE', 85.80)
>> tuple_length = len(student_data)
>> print('Length of the tuple is, tuple_length )
Length of the tuple is 5
Finding the Maximum and Minimum Value in
a Tuple
• The Python programming language provides built-in methods max( ) and min( ) to
find the maximum and minimum elements in a tuple. For example:
>> my_tuple = (12, 2, 5, 90, 30, 40, 3)
>> print('The maximum value in the tuple is ’, max(my_tuple))
>> print('The minimum value in the tuple is ’, min(my_tuple))
The maximum value in the tuple is 90
The minimum value in the tuple is 2
divmod() Function
• The divmod() function takes two numbers and returns a pair of numbers (a tuple)
consisting of their quotient and remainder.
• The divmod() returns
• (q, r) - a pair of numbers (a tuple) consisting of quotient q and remainder r.
• Example:
>>print('divmod(3, 8) = ', divmod(3, 8))
>>print('divmod(5, 5) = ', divmod(5, 5))
divmod(3, 8) = (0, 3)
divmod(5, 5) = (1, 0)
Tuple Methods
• Methods that add items or remove items are not available with tuple. Only the
following two methods are available.
Sr.No. Function with Description
1 count(item): Returns the count of number of items passed as an argument.
2 index(obj): Returns the index of the first matched item.
count() Method
• The count() method returns the number of occurrences of an element in a tuple.
• In simple terms, count() method searches the given element in a tuple and
returns how many times the element has occurred in it.
• Example:
>>thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
>>x = thistuple.count(5)
>>print(x)
2
index() Method
• The index(x, start, end) method searches an element in a tuple and returns its index. In simple terms, index()
method searches for the given element in a tuple and returns its position. However, if the same element is
present more than once, the first/smallest position is returned. For example:
>>thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
>>x = thistuple.index(8)
>>print(x)
3
>> thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
>> for i in range(len(thistuple)):
if(thistuple[i]==8):
x = thistuple.index(8,i)
print(x)
3
8
Nested Tuples
• A tuple which contains another tuple as its element, it is called nested tuple. For
example, the following diagram illustrates the nesting of a tree of Tuples:
• tuple_nested = (42,(1,3,5,7),(‘John’,’Denise’,’Phoebe’,’Adam’),5.5)
Accessing Nested Tuple Elements
• Each element of the tuple has an index value, which means each sub-tuple has a
index that starts with 0.
• Example:
>>Tuple = (“Python”, (2, 4, 6), (4, 5.6, “Hi”))
>>print(“Second item of First element of the tuple is:”, Tuple[0][1])
>>print(“Items present inside another list or tuple is:”, Tuple[2][1])
Second item of First element of the tuple is : ‘y’
Items present inside another list or tuple is: 5.6
Nested Tuples – Example Program
• Given a tuple pairs = ((2,5),(4,2),(9,8),(12,10)), count the number of pairs (a,b)
such that both a and b are even.
>> tup = ((2,5),(4,2),(9,8),(12,8))
>> count = 0
>> for i in range (len(tup)):
if tup [i][0] % 2 == 0 and tup[i][1] % 2 == 0:
count += 1
>> print("The number of pair (a,b) such that a and b are even:",count)
The number of pair (a,b) such that a and b are even: 2
References
• Think Python: How to Think like a Computer Scientist by Allen B. Downey, Publisher: O'Reilly
Media; 2 edition (December 28, 2015).
• Starting Out with Python by Tony Gaddis, Pearson; 4 edition (March 16, 2017)
• https://www.programiz.com/python-programming/tuple
• https://www.tutorialspoint.com/python/python_tuples.htm
• https://link.springer.com/chapter/10.1007/978-3-030-20290-3_31