Python Labs : Basic Data Structures [Tuples]
In this article, I will explain what is a tuple and how to implement it with Python.
What is a tuple?
First of all, a tuple is a type of data structure. Similar to a list, a tuple is also an ordered collection of objects/values. Yet it is unchangeable. That means, once a tuple is created, you cannot change its values. If you have gone through my article, "Python Labs — Basic Data Structures [Lists]" or if you have a better understanding of list, in summary, Tuples are identical to lists in many aspects, except for the following two facts:
- Instead of square brackets like in lists, tuples are defined by enclosing the elements/items in parentheses (()).
- Tuples are immutable/unchangeable. You will hear the word 'immutable' a lot when you work with tuples, so, I thought of using it here.
Let's get into some sample codes and then discuss further afterwards. Following sample code (Sample_Code_01) will explain how to define a tuple and call the values inside.
One of the famous usages with tuples is they are used to store fixed coordinates like x, y and z. coordinate1 = (7, 8) or coordinate2 = (10, 11, 12)
. There are many more examples, another common usage is, in the Python console as in Figure_01 below, Python displays responses in parentheses because it is implicitly interpreting the input as a tuple ('hello', 1.1, 100)
. So it's not only moms who store their food preferences in tuples 😜 as in the Sample_Code_01.
N.B. Contradicting to the characteristic that I have mentioned above in this article, in Sample_Code_01, you will see that I have defined moms_food_preference
without using parentheses '()'. Well, Python sometimes acts strange 😝. There are a handful of other situations like this. In here also Python allows the parentheses to be left out yet identify it as a tuple instead of a list.
All right, I hope you have an idea on how to define a tuple. Let's see some functions and packing and unpackings you can do with tuples. Refer the Sample_Code_02 below.
Time to see what are the characteristics of tuples. Well, the good news is all the characteristics that I have discussed under the list here are applicable to tuples as well except the fact that they can’t be modified.
Then you might be thinking why use a tuple instead of a list.
- The first thing is the execution time. When you use tuples program execution will be faster compared to a similar size of a list. This is unnoticeable when the size of the list and tuple are too small though.
- Secondly, if you need your collection to be remained unmodified throughout the life of the program then using a tuple is a better idea than a list. This could prevent, accidental as well as unauthorised modification to the program data.
- You can use tuple in the data type call Dictionary. Refer “Python Labs — Basic Data Structures [Dictionary]” for further reading.
Additional Information for your knowledge
Before the conclusion, I'd like to draw your attention to some peculiarity regarding tuple definition. This is actually when defining a tuple with a single item/element. Of course, there's no ambiguity when you define an empty tuple or tuple with 2 or more items. Python recognizes that you define a tuple without a problem but not when there's only a single item. See the Sample_Code_03 below.
Here the problem is, even for operator precedence in expressions, we use parentheses. Therefore when you define sample_tuple = (1)
Python recognises it as an int
instead of a tuple
.
That's Tuples in a nutshell. When you are doing more coding you will get used to these data types more and more. If there are any questions please feel free to comment them below and as always don't forget to add some feedback for me. Happy learning! 😃.
If you like this article and the content that I create, you can buy me a virtual coffee ️😊;
Cheers!