Python tuple functions


Tuple Functions and Methods

  1. Tuple Methods:

    • tuple.count(x): Returns the number of occurrences of x in the tuple.
    • tuple.index(x[, start[, end]]): Returns the index of the first occurrence of x in the tuple.
  2. 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.
  3. 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/FunctionDescription
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.