Loading video player…

Getting Started With Tuples

00:00 So what exactly is a tuple? In Python, a tuple is a built-in data type, meaning it’s always available to you, the programmer. It’s a kind of sequence data type, meaning it’s a container, and it’s marked by values enclosed in parentheses.

00:14 In fact, as a container type, it’s the default in Python, meaning any unenclosed comma-separated sequence, as long as the syntax is correct, will be handled as a tuple.

00:26 But wait, there’s more. Tuples have a number of other important characteristics that distinguish them from similar data containers in Python. Tuples are ordered.

00:36 They contain elements arranged sequentially in a fixed order determined at the time of creation.

00:42 Tuples are indexable, meaning they allow element access by zero-based integer indices. This means that the element at index zero will be the first element of the tuple.

00:52 Tuples are sliceable. They support Python slice notation to retrieve a series of elements, which itself will be another tuple. Tuples are immutable, meaning they cannot grow or shrink, and elements cannot be reassigned.

01:05 But as you’ll see in a future lesson, this doesn’t mean that tuples will always hold the same data. Because they’re unchanging, they’re able to be optimized to use less memory.

01:14 This makes tuples lightweight. They’re heterogeneous, meaning they can hold elements of differing types, including other tuples. Tuples are iterable, meaning they conform to the standard Python interface for iterable objects and can be traversed using loops and comprehensions.

01:31 Tuples are also combinable. They can be combined using concatenation, resulting in a new tuple. And finally, tuples are hashable. As long as the elements inside the tuple are themselves immutable, the tuple can be hashed, which is a requirement of keys in a Python dictionary.

01:48 We’ll be exploring each of these concepts in depth over the next few lessons with examples in the REPL.

01:54 But first things first, you’ll need some tuples to work with. So your next stop, how to create a tuple. See you there.

Become a Member to join the conversation.