Python

Constant dripping wears away a stone

Tuples


Atuple in Python is used to store a sequence of multiple items. The items of a tuple may be of the same or different data types, and they are enclosed with parentheses and are separated with commas.

  • Tuple Properties:
    • Ordered: Tuples are ordered. The items in a tuple are in a defined order, and the order will not change.
    • Unchangable: Unlike lists, tuples can not be changed. You can not add more items to a tuple, change the items in a tuple, or remove items from a tuple.
    • Allow Duplicates: Items in a tuple can be duplicates.
  • Access Tuple Items:
    • Positive indexing: A tuple item can be accessed by referring to its index number in the tuple. The index of the first item is 0, the index of the second item is 1, and so on. If the index is out of range, report the errorIndexError.
    • Negative indexing: Different from positive indexing, negative indexing begins from the end to the beginning. -1 refers to the last item, -2 refers to the second to the last item, and so on. If the index is out of range, report the errorIndexError.
    • Range indexing: Multiple items can be accessed by using a range of indices to specify where to start and where to end. For example, a_tuple[start:end] is a new tuple that includes all the items that are referred to bystart(inclusive) up toend(exclusive).
  • Tuple Operations:
    • Update Tuples: Tuples are unchangable, but you can convert the tuple that needs to be updated to a list first. Then update the list and convert the list back to a tuple.
    • Unpack Tuples: When we create and assign values to a tuple, it is called packing a tuple. To extract the values of a tuple to variables is called unpacking a tuple. When the number of items in the tuple is greater than the number of variables when unpacking, use a * in front of a variable name. The corresponding values will be assigned to the variable as a list.
    • Join Tuples: Two or more tuples can be joined together as one tuple using the+operator. If repeat the content of a same tuple multiple times, use the* operator.
  • Tuple Methods:
    • count(val): Returns the number of elements with the specified valuevalin the given tuple.
      • val: Required. A value to be searched in the tuple.
    • index(val): Returns the index of the first element with the specified valuevalin the tuple.
      • val: Required. A value to be searched for indexing in the tuple.
  • Built-in Functions with Tuple:
    • all(atuple): Returns True if all elements in the tupleatupleare true or the tuple is empty; otherwise, returns False.
      • atuple: Required. A tuple.
    • any(atuple): Returns True if any element in the tuple is true. If the tuple is empty or no element in the tuple is true, returns False.
    • enumerate(iter[, start]): Adds a counter that starts withstartto the iterableiter.and returns it as an enumerating object. Ifstartis not given, use 0 to start the counter.
      • iter: Required. An iterable.
      • start: Optional. A number from which the counter starts. By default, it is 0.
    • len(atuple): Return the number of items in the tupleatuple.
    • max(atuple): Return the maximum element in the tupleatuple.
    • min(atuple): Return the minimum element in the tupleatuple.
    • sum(iter[, start=0]): Adds all items of the iterableiterandstart and returns the sum. Ifstartis not given, use 0.
      • iter: Required. An iterable whose items should be numbers.
      • start: Optional. A number whose value is to be added to the sum of all items in the iterable. By default, it is 0.
    • sorted(iter[, key=func_name, reverse=False]): Sorts the tupleiterusing the sorting criteria specified by the functionfunc_nameand returns the sorted tuple. Ifreverseis True, sort the tuple in descending order; otherwise, sort the tuple in ascending order.
      • iter: Required. An iterable to be sorted.
      • key: Optional. A function that specifies the sorting criteria.
      • reverse: Optional. True or False. If it is True, sort the iterable in descending order.
  • Operators with Tuple:
    • + (concatenation): It appends all items in the right operand tuple to the end of the left operand tuple and returns a new tuple. The two original tuples are unchanged.
    • * (Repeating Tuple Elements): Create a new tuple with the items in the given tuple repeated the specific number of times.