Online-Academy
Look, Read, Understand, Apply

Tuple

Tuple

Tuple is an ordered collection data type used to store mulitple values in a single variable, like a list, but tuples are immutable (cannot be changed after creation).
A tuple is an ordered and immutable collection of elements in Python, used to store multiple values in a single variable. It allows duplicate values. Items of tuples are indexed, the first item has index [0], second has [1] and so on.
Tuples are created using round brackets. We cannot change, or add, or remove items from tuple. -- Immutable.

        t = (10,20,30)
        print(t)
        print(type(t))

    Single-element tupe 
    
    t = (111,)  #there must be a comma after a value
    print(t)    #if comman is not written, Python will treat it as an integer

    Tuple can have different data types:
    person  = ("Dinesh",25,5.66,True) #A tuple can store heterogeneous data.
    print(person)

    Accessing Tuple Elements
    t = (100,200,300,400)
    print(t[0])     # displaying First element
    print(t[-1])    # displaying Last element

    Slicing Tuple

    t = (10, 20, 30, 40, 50)

    print(t[1:4])   # (20, 30, 40)
    print(t[:3])    # (10, 20, 30)
    print(t[::2])   # (10, 30, 50)

    Checking if an item is present in tuple:

    t = ("ET","Rocky","Rambo","Equalizer","MI")
    if "Equalizer" in t:
        print(f"Equalizer is in tuple")

    Tuple is Immutable 
    t = (10, 20, 30)
    t[1] = 99   # Error
    
    Looping Through a Tuple
    t = ("Python", "Java", "C++")

    for lang in t:
        print(lang)
    Updating Tuple Items
    We cannot direct change items in tuple but we can change items by creating list from tuple, changing list, assigning list to tuple.

    t = ("ET","Rocky","Rambo","Equalizer","MI")
    l = list(t)  #passing tuple to list constructure
    l[0] = "Titanic"  # changing list item
    t = l   # Assigning list to tuple
    print(t)

    This same technique is used to add or remove item from tuple.

    Adding tuple to a tuple is possible:
    t = ("ET","Rocky","Rambo","Equalizer","MI")
    t1 = ('Top Gun',"GodFather")
    t += t1
    print(t)

    

    Tuple Functions
    t = (5, 10, 15, 10)

    print(len(t))     # Length
    print(max(t))     # Maximum value
    print(min(t))     # Minimum value
    print(t.count(10)) # Count occurrences
    print(t.index(15)) # Index of element

    Tuple Packing and Unpacking
    data = 10, 20, 30

    Unpacking Tuple
    a, b, c = data
    print(a, b, c)

    Tuple in Function
    
    def calculate(a, b):
        return a + b, a - b

    result = calculate(10, 5)
    print(result)

    Unpacking return values:
    
    sum, diff = calculate(10, 5)
    print(sum, diff)

    Using Asterisk * 

    t = (2,3,4,5,6)
    (one, *two, three) = t
    print(one)
    print(two)
    print(three)

    Output will be: 

    2      # content of one
    [3, 4, 5]  #content of two
    6       # content of three

    

Characteristics of Tuple

  • Ordered collection
  • Immutable (read-only)
  • Allows duplicate values
  • Can store different data types
  • Written using parentheses
FeatureTupleList
Syntax()[]
MutableNoYes
FasterYesSlower
Safe from ChangeYesNo
When to use Tuple?
  • If data does not change
  • To store fixed records like coordinates, database rows
  • If better performance is need
  • for function return values