In this lesson, we’ll explore tuple objects a bit closer and we’ll cover the ways to create and use tuples. This course is part of a series of content designed to help you learn to program with the Python programming language.
- Tuples are a special type of collection used to store a small number of related objects
- Tuples cannot be changed once they’re created
- Tuples can be created by:
- Passing a collection to the built-in tuple function
- Using the parentheses shorthand syntax with each object separated with a comma
- Tuples can be automatically unpacked into name bindings by specifying the same amount of names as objects
This course was designed for first-time developers wanting to learn Python.
This is an introductory course and doesn’t require any prior programming knowledge.
Hello, and welcome! My name is Ben Lambert, and I’ll be your instructor for this course. This course is part of a series of content designed to help you learn to program with the Python programming language.
Should you wish to ask me a specific question, you can do that with the contact details on screen. You can also reach support by using the email address: email@example.com. And one of our cloud experts will reply.
In this lesson we’ll explore tuple objects a bit closer. We’ll cover the ways to create and use tuples. The tuple object is conceptually similar to a list object. They both contain a collection of objects. However, tuples are much more limited. When using a list we can add and remove objects as needed.
For example, if we create a TODO list we might add tasks such as walk the dog or learn Python. And once those are complete, we want to remove those tasks. Tuples are not that flexible. Once created tuples can’t be changed.
Let’s talk about how each is commonly used. It’s possible to create a list where we specify all the contained objects in code.
For example: we could create a list containing a few options. It’s not uncommon to know the values of your list when you’re writing code. However, it’s perhaps not the most common use.
It’s more common that the lists we use will be populated by external data, such as a file or database. Once we have a list populated with objects, we’ll perform operations based on what we’re trying to accomplish.
Perhaps we get a list of numeric results that we need to summarize. Or maybe we get a list of users from a database and we want to loop over each user and determine if the user’s account is active. Lists are dynamic and useful for storing both known and unknown amounts of objects, depending on how the list is populated.
Now let’s see how tuples compare. Tuples are intended to store a limited collection of related objects. For example imagine we’re working with images and we want to know the dimensions of an image. An image’s dimensions is a singular concept which consists of two pieces of data, the width and height.
This is the intent of tuples. They’re a small collection of related objects. Because tuples cannot be modified once created, they’re more of a developer convenience than anything else. They enable us as developers to easily represent a single concept with a few objects.
Every object that we create takes up space in memory. Every attribute and method consumes some space. Using memory when we need to is fine. However, for some specific use cases such as our dimensions example, all we really need is a tuple to store two numbers. Which makes tuples a more memory efficient option than creating a custom object.
Tuples can be created using the built-in tuple function. Calling this function with no arguments will create an empty tuple. In reality, this isn’t that useful since we can’t change a tuple after it’s created. However, the function also accepts a collection as input and will convert it to a tuple. For example we could pass the function a list and it’ll create a tuple. The shorthand syntax for tuples uses parentheses. Each object in the tuple is separated by a comma.
Similar to lists, tuples maintain the order of objects. This is a really important feature of tuples. Imagine that we wish to resize an image. So we have a tuple storing the dimensions. The first represents an image’s width, the second represents the height.
The order of these is important because if they can move position then our images will end up misshapen. Once we have a tuple we can access its objects by using their index. This is similar to lists.
Accessing each individual object using its index isn’t difficult, however, Python provides an easier way to access the values in a tuple. Python has a feature called tuple unpacking which enables us to bind a name to each object in a tuple.
Thus far when we’ve bound names to objects, we’ve used one name. However, in this example we’re binding both the width and height at the same time. Python will automatically unpack the tuple so that each name is bound to the object in the same relative position.
After this line is interpreted the width will be bound to the first object and the height to the second. There are some rules regarding how tuples are unpacked. If the number of objects and names don’t match the interpreter will throw an error.
Similar to lists, sets, and dictionaries, we can use the in and not in operators to determine if an object does or does not exist in a tuple. Tuples have some great use cases. However, know that those use cases are limited.
In fact, if you forget everything else from this lesson except the notion of tuple unpacking, then it’s probably okay at this stage. Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:
Tuples are a special type of collection used to store a small amount of related objects.
Tuples cannot be changed once they’re created.
Tuples can be created by:
Passing a collection to the built-in tuple function.
Using the parentheses shorthand syntax with each object separated with a comma.
Tuples can be automatically unpacked into name bindings by specifying the same amount of names as objects.
Alright, that's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!
Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.