Tuple - Python

  • Like lists but Immutable
  • varT = (value1, value2, value3) => Initialize a tuple
    • varT = (value) => Considered as a integer
    • varT = (value,) => Considered as a tuple
  • varT[N] => Returns value of N-1 element
    • varT[-N] => Accessing element of (len(varT)-N-1) position
  • Slicing
    • varT[N1:N2:N3] => Access elements from N1 to (N2-1) jumping N3 indexes
  • varT3 = varT1 + varT2 => Concatenation of the tuples
  • len(varT) => Returns length of tuple
  • varT.count(value) => Returns the count of value
  • varT.index(value) => Returns index of first occurrence of the value
    • varT.index(value, N1, N2) => Starting & end index are N1 & N2
    • If value is not present then gives value error
  • max(varT) => Returns max element
  • min(varT) => Returns min element
  • w, x, y,..... = varT => Unpacking the tuple into the variables
  • Checks if element is present in the tuple
        if value in varT:
            # statements
        else:
            # statements
    
  • Change a tuple by converting into list
        varT = (value1, value2)
        temp = list(varT)
        # statements
        varT = tuple(temp)
    
Share: