Learn how to store data in a single variable using Tuples!
Oftentimes, programmers want to store a collection of values in a single variable. In many cases, programmers use lists to store data:
Lists are especially useful for data that may change over time - grocery lists are constantly changing depending on how much food is left in the fridge - but what if we had data that wasn’t expected to change at all over time?
Suppose we had a graph comparing households and the number of times they took trips to the grocery store from the 1920’s - that data isn’t going to change at all since the data is from a year that already happened. Data that is unchanging, or immutable, is often best stored in a data structure called a tuple.
Tuples are an ordered sequence of heterogenous items that cannot be changed once they are initialized. Like strings, their values can be used to create other tuples, but the individual values within a tuple will always remain the same.
To initialize a tuple, values to be included in the tuple can be wrapped in parenthesis:
They can also be written without parenthesis:
To make a tuple of only one value, a comma must be included after the value:
Otherwise python will assume that the value is a primitive type.
Similar to lists and strings, values in a tuple can be accessed through indexing. Simply specify the index that you’d like to access using the [ ] notation:
To determine if a value is stored in a tuple, we can use the index()
function to return the index of a given value:
If the value is not in the tuple, then the value -1 will be returned.
A science class paired students together for the entire school year and stored each pairing in a tuple. Unfortunately two of the students moved to Arkansas in the middle of the year, the pairings had to be altered.
From the tuples provided, index into the two tuples that have the names “Ernesto” and “Gabby”, and store those names in a new tuple. Then, print the new tuple to the console.
Although tuples cannot be changed, tuples and their values can used to create other tuples. Tuples can be added together to form a larger tuple:
Each value in a tuple can also be separated and stored in an individual variable, making it easy to use the content of a tuple without having to change it:
This technique is referred to as sequence unpacking - the values in the tuple are stored in the variables in the order that the variables are declared.
A bookstore used to have only one copy of a rare collector’s edition of “Alice in Wonderland”. Recently they got two more copies! First, create a new tuple that includes two copies of “Alice in Wonderland”, then create another tuple that includes “Alice’s Adventures in Wonderland” (one of the copies was a special edition).
Similar to lists, values in tuples can be iterated over using a for-in loop:
A zoo stores all of the animals that it has in a tuple. However, they decided to loan all animals whose name start with a C to the Crazy Cool Circus Collective. Using a for loop, iterate through the tuple to count how many animals the zoo needs to loan out to the circus.