You can learn about Tuples in Python Programs with Outputs helped you to understand the language better.
Python Programming – Tuple Functions
Unlike Python lists, tuples do not have methods/functions such as append ( ), remove ( ), extend ( ), insert ( ) and pop( ) due to their immutable nature. However, there are many other built-in methods to work with tuples: count ( ) ,len( ), max( ), min( ), etc.
Recommended Reading On: Java Program to Print Series 5 25 125 625 3125…N
cmp ( )
cmp ( ) does not exist in Python 3. If you really want it, you can define it yourself, and it gives the same result. For example,
def cmp( a , b ):
return (a > b) – (a < b)
In Python 2, cmp ( ) function compares elements of both tuples. It checks whether the given tuples are the same or not. If both the tuples are the same, it will return 0, otherwise 1 or -1. If the first tuple is bigger than the second one then it returns 1, otherwise -1.
Syntax:
cmp (tup1,tup2) #tup1 and tup2 are tuples
Example
>>> tup1= ( 11 , 22 , 33 )
>>> tup2= ( 50 , 60 , 70 )
>>> tup3= ( 11 , 22 , 33 )
>>> cmp(tup1,tup2) # first tuple is small
-1
>>> cmp(tup1,tup3) //same
0
>>> cmp(tup2,tup1) //first tuple is big
1
- Python Programming – Tuple
- Python Programming – Creation, Initialization and Accessing The Elements In A Tuple
- Python Programming – Standard Data Types
count ( )
count ( ) returns the number of occurrences of an item in the tuple. For example,
a = ( 1 , 2 , 2 , 3 , 4 , 5 , 5 , 5 )
a . count ( 2 )
2
a . count ( 5 )
3
- Use the count ( ) method to calculate the number of times a specified value appears in the tuple.
len ( )
The method len ( ) returns the number of elements in the tuple.
Syntax:
len ( tup )
Example:
>>> tup1 = ( 11 , 22 , 33 )
>>> len ( tup1 )
3
max( ) and min( )
The max ( ) and min ( ) built-in functions used to find the largest and the smallest elements in a sequence, respectively. For strings, the comparison performed is alphabetical, e.g., “Anu” comes before “Zyan”. So, “Anu” would be less than “zero”. For numbers, the comparison is numeric, e.g., 10 comes before 20. These comparisons are logical.
Syntax:
max(args) #Returns the maximum of a
sequence or set of arguments
min(args) #Returns the minimum of a
sequence or set of arguments
Example:
>>> max ( 10 , 20 , 30 )
30
>>> min ( 10 , 20 , 30 )
10
>>> students = (“shiv”, “vivek”, “neelam”, “shashi”)
>>> max(students)
vivek
>>> min(students)
neelam
Example
Program that uses max ( ) and min ( ) functions.
# Use of max() and mint) for strings friends = (“sanjay”, “morris”, “ajay”, “suman”) print(max(friends)) print(min(friends)) # Max and min for numbers earnings = (1000, 2020, 500, 400) print(max(earnings)) print(min(earnings))RUN >>> Suman Ajay2020 400 >>> |
Example
Program that uses max ( ) function to get maximum value in tuples.
tup_num = ( 23 , -4 , 55 , 550 , 250 , 505 ) tup_str = ( ‘Mona’, ‘Monu’, ‘Mohan’, ‘Meena’ ) print ( “The maximum value in numbered tuple = “, max(tup_num)) print ( “The maximum value in string tuple = “, max(tupstr))RUN >>> The maximum value in numbered tuple = 550 The maximum value in string tuple = Monu >>> |
Example
Program that uses min( ) function to get minimum value in tuples.
tup_num = ( 23 , -4 , 55 , 550 , 250 , 505 ) tup_str = (‘Mona’, ‘Monu’, ‘Mohan’, ‘Meena’) print (“The minimum value in numbered tuple = “, min(tup_num)) print (“The minimum value in string tuple = “, min(tupstr))RUN >>> The minimum value is numbered tuple = -4 The minimum value in string tuple = Meena >>> |
Let’s Try
Type the following Python codes and write the out-put:
a= ( 10 , 20 , 30 , 40 , 50 ) print ( len ( a ) ) print ( min ( a ) ) print ( max ( a ) ) |
tup_sum = ( 23 , -4 , -5 , 55 , 25 , 50 ) print ( max (tup_num) ) print ( min (tup_num) ) |
sorted ( )
This function returns a sorted version of the tuple. The sorting is in ascending order, just like in the following example:
a = (6,7,4,2,1,5,3)
sorted(a)
[1, 2, 3, 4, 5, 6, 7]
- the sorted ( ) function doesn’t modify the original tuple.
tuple ( )
It is used to create tuples from different types of values.
# Creating empty tuple >>> t = tuple ( ) >>> t ( ) |
# Creating tuple from string >>> t = tuple ( ” xyz ” ) >>> t ( ‘ X ‘ , ‘ y ‘ , ‘ Z ‘ ) |
# Creating tuple from list >>> t = tuple ( [ 2 , 4 ,6 ] ) >>> t ( 2 , 4 , 6 ) |
# Creating tuple from dictionary >>> t = tuple ( { 1 : ” 1 ” , 2 : ” 2 ” } ) >>> t ( 1 , 2 ) |
- The empty tuple is formed with a pair of parentheses containing nothing.
index ( )
The index ( ) method works with tuple the same way it works with lists. It takes one argument and returns the index of the first appearance of an item in a tuple. If you use an index ( ) on a value that is not found, an error results.
Syntax:
<tuplename>.index(<item>)
Example:
>>> tup= ( 2 , 5 , 7 , 7 , 2 , 1 )
>>> tup . index ( 7 )
2
>>> tup . index ( 2 )
- Use index ( ) method to get first occurrence of the specified value in the tuple.
Let’s Try
Write the output for the following Python code:
# Get index of element with value ” ball ” . items = (“bat”, “ball”, “chess”) index = items.index(“ball”) print(index, items[index]) |
sum ( )
The sum( ) returns the arithmetic sum of all the items in the tuple.
>>> tup=(2,5,7,7,2,1)
>>> sum(tup)
24
You can not apply sum( ) function on a tuple with strings, it will generate a TypeError error:
>>> tup2=(‘2′,’5,,’7′,’7′,’2′,’1’)
>>> sum(tup2)
Traceback (most recent call last):
File “<pyshell#8>”, line 1, in <module> sum(tup2)
TypeError: unsupported operand type(s)
for +: ‘int’ and ‘str’
>>>