1. Tuples
In this chapter, we will expand on your knowledge of data structures. The first data structure that we will discuss is tuples.
2. Tuples - introduction
A tuple is an immutable data structure, meaning the values cannot be changed after creation. The sequence of values stored in a tuple can be of any type, and they are indexed with integers, just as with vectors.
3. Tuples - why?
Why might we want to use a tuple? Firstly, if you're looking to store data that should not be changing, consider using a tuple. Their immutability gives you confidence in their values.
A tuple is also faster than a vector, and this generally holds true for immutable structures vs mutable structures. We will examine the execution speed of code later.
With the benefits of immutability also come the disadvantages. We can't sort a tuple, nor can we append, delete, or change the values inside a tuple once it has been declared.
Choosing the right data structure is key!
4. Tuples - syntax
We create a tuple using parenthesis to enclose our values, separating each value with a comma.
Let's create a tuple called my_tuple with four integer values within it - 10, 20, 30, and 40.
We can also create a tuple with mixed data types. my_mixed_tuple demonstrates this, containing integer, string, and boolean values.
5. Tuples - indexing
Tuples are indexed via integer indexing starting with one, just like vectors. Accessing the first index of 'my_tuple' returns the value 10, the first element in my_tuple.
Just as with vectors, a range of elements can be extracted from a tuple by passing a range within square brackets. Slicing my_tuple using the second and third index returns the second and third values in the tuple, 20 and 30.
We can also use loops to iterate over a tuple, just as we did previously with a vector.
6. Tuples - immutability
One of the key differences between tuples and other structures is the immutability of the tuple. We can not add or remove values to a tuple after creating it, and we can not modify values within it.
If we try and use the append function to append a value to our tuple, we can see that no such method exists for the tuple data type.
We have the same error if we try and modify the first element of a tuple by changing the value to 15 from ten.
7. Tuples - immutability
Another way to look at the immutability of a tuple is by looking at the data type using the typeof function.
We have defined a vector called my_vector and a tuple called my_tuple.
If we print the data type of the vector first, we get a vector of integers.
However, if we print the data type of the tuple, we get a different result. We can see that it is a tuple; however, the type definition also shows that it contains five integer values. This demonstrates how the tuple keeps track of the number of elements within it, as that number cannot change throughout the life of the tuple.
8. Tuples - NamedTuple
One final tuple is a variant called a NamedTuple. It is similar to the previous tuple, except that each element within a NamedTuple has a unique 'key' or 'name' associated.
Other than this unique method of accessing the values within a NamedTuple, they are the same.
We have created a named tuple with three elements. Each element has its unique key - the name, the country, and the city.
To access the name within the NamedTuple, we could print the first element, which would give us the name. However, we can also access it using the unique key 'name'. Both methods provide the same result, but using the name key is more intuitive.
9. Let's practice!
You've seen a lot of tuples, now let's practice.