Python tuple functions
Tuple Functions and Methods
Tuple Methods:
tuple.count(x)
: Returns the number of occurrences ofx
in the tuple.tuple.index(x[, start[, end]])
: Returns the index of the first occurrence ofx
in the tuple.
Common Built-in Functions:
len(tuple)
: Returns the number of elements in the tuple.max(tuple)
: Returns the largest element in the tuple.min(tuple)
: Returns the smallest element in the tuple.sum(tuple)
: Returns the sum of all elements in the tuple (works with numeric tuples).sorted(tuple)
: Returns a new sorted list of the elements in the tuple.
Tuple Operations:
- Slicing: You can create a new tuple from a portion of an existing tuple using slicing.
- Concatenation: You can join two tuples using the
+
operator.
Summary of Methods and Functions
Method/Function | Description |
---|---|
tuple.count(x) | Count occurrences of x in the tuple. |
tuple.index(x) | Find the index of the first occurrence of x . |
len(tuple) | Get the number of elements in the tuple. |
max(tuple) | Get the maximum value in the tuple. |
min(tuple) | Get the minimum value in the tuple. |
sum(tuple) | Calculate the sum of numeric values in the tuple. |
sorted(tuple) | Get a new sorted list from the tuple. |
These functions and methods allow you to work effectively with tuples in Python, providing ways to manipulate and retrieve information from them while respecting their immutability.