☞ 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 :T = ( 10, 20, 30, 40, ("inspire", "web", "soft"), [222,111] ) print(len(T))
☞ count() : It is used to count the occurrences of an element in the tuple.
Syntax :T = ( 10, 20, 30, 40, ("inspire", "web", "soft"), [10,20], 55, 10, 10 ) print(T.count(10))
☞ 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> )
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))
0 3 3 3
T = ( 10, 20, 30, 10, 40, ("inspire", "web", "soft"), [10,20], 55, 10, 10 ) print(T.index(10, 1, 3))
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 :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))
False True True
☞ min() : It returns the element with minimum value from the tuple.
Syntax :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))
10 A A AB 10.0
☞ max() : It returns the element with maximum value from the tuple.
Syntax :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))
147 Z z za 147.5
☞ sum() : It returns the sum of elements of the tuple.
Syntax :T1 = ( 10, 20, 65, 55.5, 147, 30) print(sum(T1))
☞ sorted() : It returns the list after sorting.
Syntax :sorted( <tuple> ) #Ascending order sorted( <tuple> , reverse=True) #Descending order
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))
[10, 20, 30, 55.5, 65, 147] [147, 65, 55.5, 30, 20, 10] ['A', 'W', 'Z'] ['Z', 'W', 'A']