How do i use tuples in python?



  • Tuples are the same as lists. Instead of using square brackets they use parentheses, also known as round brackets.

    Note: Tuples are immutable when I try to change the value of index zero below, it would not allow it, and instead printed the error; "object does not support item assignment".

    Define a tuple.

    >>> characters = ("goofy", "daffy", "popeye")
    >>> print(characters)
    ('goofy', 'daffy', 'popeye')
    

    Try to assign another value to index zero, changing it from goofy to olive.

    >>> characters[0] = "olive"
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
    

Log in to reply
 

© Lightnetics 2024