Some Functions of Tuple


☞ Following are the some functions of Tuple.

1. len()
2. count()
3. index()
4. any()
5. min()
6. max()
7. sum()
8. sorted()

len() : It returns the length of the tuple.

Syntax :
len( <tuple>)
Example :
 
T = ( 10, 20, 30, 40, ("inspire", "web", "soft"), [222,111] )
print(len(T)) 
Output
6

count() : It is used to count the occurrences of an element in the tuple.

Syntax :
<tuple>.count( <element> )
Example :
 
T = ( 10, 20, 30, 40, ("inspire", "web", "soft"), [10,20], 55, 10, 10 )
print(T.count(10)) 
Output
3

index() : It finds the first index of a given element and returns the index.

Syntax :
 
<tuple>.index( <element> )
<tuple>.index( <element>, <start_index> )
<tuple>.index( <element>, <start_index>, <stop_index> )
Example 1 :
 
T = ( 10, 20, 30, 10, 40, ("inspire", "web", "soft"), [10,20], 55, 10, 10 )
print(T.index(10))
print(T.index(10, 2))
print(T.index(10, 2, 9))
print(T.index(10, 2, 15))
Output
0
3
3
3
Example 2 :
 
T = ( 10, 20, 30, 10, 40, ("inspire", "web", "soft"), [10,20], 55, 10, 10 )
print(T.index(10, 1, 3)) 
Output
print(T.index(10, 1, 3))
ValueError: tuple.index(x): x not in tuple

any() : It returns True if a tuple consists of at least one element, Otherwise False.

Syntax :
any( <tuple> )
Example :
 
T1 = ()
print(any(T1))

T2 = ( 10, )
print(any(T2))

T3 = ( 10, 20, 30, 10, 40, ("inspire", "web", "soft"), [10,20], 55, 10, 10 )
print(any(T3)) 
Output
False
True
True 

min() : It returns the element with minimum value from the tuple.

Syntax :
min( <tuple> )
Example :
 
T1 = ( 10, 20, 65, 55, 147, 30)
print(min(T1))

T2 = ( "Z", "A", "W" )
print(min(T2))

T3 = ( "A", "a", "Z", "z" )
print(min(T3))

T4 = ("AB", "Ab", "aB", "ZA", "zA", "za" )
print(min(T4))

T5 = ( 10.0, 20.5, 65, 55, 147.5, 30)
print(min(T5)) 
Output
10
A
A
AB
10.0

max() : It returns the element with maximum value from the tuple.

Syntax :
max( <tuple> )
Example :
 
T1 = ( 10, 20, 65, 55, 147, 30)
print(min(T1))

T2 = ( "Z", "A", "W" )
print(min(T2))

T3 = ( "A", "a", "Z", "z" )
print(min(T3))

T4 = ("AB", "Ab", "aB", "ZA", "zA", "za" )
print(min(T4))

T5 = ( 10.0, 20.5, 65, 55, 147.5, 30)
print(min(T5)) 
Output
147
Z
z
za
147.5
Note : max() and min() check for the characters in a string on the basis of their ASCII values. If there are two strings in a tuple, then the first different character in the strings is checked.

sum() : It returns the sum of elements of the tuple.

Syntax :
sum( <tuple> )
Example :
 
T1 = ( 10, 20, 65, 55.5, 147, 30)
print(sum(T1))
Output
327.5

sorted() : It returns the list after sorting.

Syntax :
sorted( <tuple> )                       #Ascending order
sorted( <tuple> , reverse=True)        #Descending order
Example :
 
T1 = ( 10, 20, 65, 55.5, 147, 30)
print(sorted(T1))

T2 = ( 10, 20, 65, 55.5, 147, 30)
print(sorted(T2, reverse=True))

T3 = ( "Z", "A", "W" )
print(sorted(T3))

T4 = ( "Z", "A", "W" )
print(sorted(T4, reverse=True))
Output
[10, 20, 30, 55.5, 65, 147]
[147, 65, 55.5, 30, 20, 10]
['A', 'W', 'Z']
['Z', 'W', 'A']