Python Tuple Functions and Methods (With Program)
Tuple in Python is an ordered and immutable collection of elements. It is written inside
parentheses (). Because tuples are immutable, their elements cannot be changed after
creation.
Tuple Functions
1. len() – Returns the number of elements in the tuple.
2. max() – Returns the largest element in the tuple.
3. min() – Returns the smallest element in the tuple.
4. sum() – Returns the sum of all elements in the tuple.
Tuple Methods
1. count(x) – Returns the number of times element x appears in the tuple.
2. index(x) – Returns the index of the first occurrence of element x in the tuple.
Program Using Tuple Functions
# Program to demonstrate tuple functions
# Creating a tuple
t = (15, 25, 10, 40, 30)
print("Tuple elements:", t)
# Using tuple functions
print("Length of tuple:", len(t))
print("Maximum value:", max(t))
print("Minimum value:", min(t))
print("Sum of elements:", sum(t))