Adam Lombard
Posted on January 19, 2020
In Python, tuples are immutable data sequences. They are typically used to group related but heterogeneous values.
Say we have several pieces of information—in a variety of data types—that together describe a single work of art:
artist = "Louise Nevelson"
title = "Sky Gate, New York"
media = ["wood", "oil paint"]
completed = 1978
It may be convenient to group these values together. Since the description of an artwork should never change during our program, an immutable tuple suits our needs perfectly:
artwork = (artist, title, media, completed)
Elements in a tuple can be accessed by index:
>>> artwork[0]
'Louise Nevelson'
But, since tuples are immutable, we cannot change elements by index:
>>> art_work[0] = "Ruth Asawa"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
For more information on tuples, see the Python docs.
Was this helpful? Did I save you some time?
🫖 Buy Me A Tea! ☕️
Learn more about Louise Nevelson and Ruth Asawa.
Posted on January 19, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.