Tuple functions in Python | Tuple methods in Python

Preview
You must first complete Python Tuple with Examples | Tuple in Python 3 before viewing this Lesson

Tuple functions in Python | Tuple methods in Python

1. len() method

This method returns number of elements in a tuple.
Syntax:
len(<tuple>)
<tuple> refers to user defined tuple whose length we want to find.
Example:

>>> T1=(10,20,30,40)
>>> len(T1)
4
#There are 4 element in tuple.



2. max()

This method returns largest element of a tuple. This method works only if the tuple contains all values of same type. If tuple contains values of different data types then, it will give error stating that mixed type comparison is not possible:
Syntax :
max(<tuple>)
<tuple> refers to name of tuple in which we want to find maximum value.
Example 1:

>>> T1=[10,20,30,40]
>>> max(T1)
40
# 40 is the maximum  value in tuple T1.

 Example 2:

>>> T2=['Sumit','Anil','Rahul']
>>> max(T2)
'Sumit'
#’Sumit’ appears as last member as per English dictionary.

 Example 3:

>>> T3=['Sumit',10,'Anil',11,'Rahul',12]
>>> max(T3)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
max(T3)
TypeError: '>' not supported between instances of 'int' and 'str'
 #Maximum can’t be found in mixed values.

Example 4:

>>> max([3,4],(5,6))
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    max([3,4],(5,6))
TypeError: '>' not supported between instances of 'tuple' and 'tuple'
 
#Maximum can’t be found among tuple and tuples.

 3. min()

This method returns smallest element of a tuple. This method works only if the tuple contains all values of same type. If tuple contains values of different data types then, it will give error stating that mixed type comparison is not possible:
Syntax :
min(<tuple>)
<tuple> refers to name of tuple in which we want to find minimum value.
Example 1:

>>> T1=[10,20,30,40]
>>> min(T1)
10
#10 is the minimum  value.

 Example 2:

>>> T2=['Sumit','Anil','Rahul']
>>> min(T2)
'Anil'
#’Anil’ appears first as per English dictionary.

 Example 3:

>>> T3=['Sumit',10,'Anil',11,'Rahul',12]
>>> min(T3)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
min(T3)
TypeError: '<' not supported between instances of 'int' and 'str'
#Minimum can’t be found in mixed values.

Example 4:

>>> min([3,4],(5,6))
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
min([3,4],(5,6))
TypeError: '<' not supported between instances of 'tuple' and 'tuple'
#Minimum can’t be found among tuple and tuples.

 4. index()

This method is used to find first index position of value in a tuple. It returns error if value is not found in the tuple.
Syntax:
Tuple.index (<Val>)
Tuple is user defined tuple.
<Val> refers to the value whose index we want to find in Tuple.
Example 1:

>>> T1=[13,18,11,16,18,14]
>>> print(T1.index(18))
1
#Index of first occurance of 18 is shown i.e. 1.

Example 2:

>>> print(T1.index(10))
Traceback (most recent call last):
File “<pyshell#2>”, line 1, in <module>
T1.index(10)
ValueError: 10 is not in tuple
# Above example shows error as 10 is not in the tuple



5. count()

This function is used to count and return number of times a value exists in a tuple. If the given value is not in the tuple, it returns zero.
Syntax:
Tuple.count(<value>)
<value> refers to the value whose count we want to find.
Example 1:

>>> T1=[13,18,11,16,18,14]
>>> T1.count(18)  #18 appears twice in tuple T1.
2

 Example 2:

>>> T1=[13,18,11,16,18,14]
>>> T1.count(30)       
0 #0 is the output as 30 doesn’t exist in the tuple T1.

 6. tuple()

This method is used to create a tuple from different types of values.
Syntax:
Tuple(<sequence>)
<sequence> refers to a sequence type object that we want to convert to a tuple.
Example 1 – Creating empty tuple

>>> t=tuple()
>>> t
()

Example 2 – Creating a tuple from a list

>>>t=tuple([1,2,3])
>>>t
(1,2,3)

Example 3  – Creating tuple from a string

>>>t=tuple(“abc”)
>>>t
(‘a’,’b’,’c’)

Example 4  – Creating a tuple from keys of a dictionary

>>> t1=tuple({1:'a',2:'b'})
>>>t1
(1,2)




Lesson tags: convert list into tuple, count number of values in tuple, covert string into tuple, find length of tuple, find maximum value in tuple, find minimum value in tuple, len() method of tuple in python, Tuple functions in Python, Tuple methods in Python
Back to: Python Programming Tutorial