tuple
C2Pronunciation
UK
- /ˈtjuːpəl/noun
- /ˈtʊpəl/noun
- /ˈtʌpəl/noun
- /ˈt͡ʃuːpəl/noun
US
- /ˈtuːpəl/
- /ˈtʌpəl/
Description
- ordered sequence
- immutable collection
- fixed grouping
Imagine you're packing for a trip. You might make a mental list: socks, toothbrush, book. That ordered sequence—socks first, then toothbrush, then book—is similar to what programmers call a "tuple." A tuple is simply an ordered collection of items. Unlike a regular list, which can change, once you create a tuple, it stays fixed!
Think of it like coordinates on a map (latitude, longitude). The order matters! (34.0522, -118.2437) tells you the location of Los Angeles, but switching those numbers wouldn't get you there. Tuples are used in computer science to group related data together—maybe a person's name and age, or the RGB values of a color. They're like sturdy little containers for keeping things organized and making sure they aren't accidentally changed.
A tuple is an ordered, immutable (unchangeable) sequence of elements. It's a fundamental concept in many programming languages, including Python, where it serves as a way to group related data together. Think of it as a container that holds multiple values in a specific, permanent order.
Unlike lists, which are mutable—meaning you can add, remove, or change items after creation—tuples are immutable. Once defined, the elements within a tuple cannot be altered. This makes them incredibly useful for representing fixed collections of data where consistency and data integrity are important.
For example, consider representing a point in 3D space. You might use a tuple like `(10, 20, 30)` to store the x, y, and z coordinates. The order is vital: (10, 20, 30) represents a completely different location than (30, 20, 10). Because tuples are immutable, you can be certain that these coordinates won't accidentally change during your program's execution.
Tuples are also frequently used to return multiple values from a single function. For instance, a function might return a tuple containing both the minimum and maximum values of a dataset. In Python, they can also be used as dictionary keys when their contents are immutable. They can improve code readability by clearly signaling to other programmers that a specific collection of items is intended to remain constant. In short, a tuple is an organized, fixed bundle of information that helps your data stay exactly where and how you put it.
Examples
- 1
Programming return
In Python, the function returns a tuple with the file name and its size.
Domain
in programming, a tuple is a small fixed set of values
- 2
Map coordinates
Each point on the map is stored as a three-number tuple.
- 3
Variable unpacking
We unpacked the tuple into separate variables before using the values.
Forms and spellings
1 form open this card.
Main spelling
- tuple