Mastery 24. Creation and use of tuples in Python

In this post I will explain you what tuples are and how to use them.

If we remember from an old post, we saw that we could group items by using lists. Tuples are just like lists, but the main difference is that once you had created a Tuple, you can not modify it, as we could with lists.

The syntax of a Tuple, different from lists where we used brackets, is the following:

     Tuple=(item1,item2,item3,itemN)

Where item1, item2, item3 and itemN can take any value, it doesn’t matter if it is a string, integer number, or even a list or another tuple, etc.

We can refer to values of a tuple as we refer to values of a list. The syntax is the following:

>>Tuple[2]

item3

Where 2 referes to the third item because it starts counting from 0.

 

We may not be able to modify Tuples but we can always modify a list inside the tuple, as soon as we dont wanted to put up any other value instead of the list itself.

 

CC BY 4.0 Mastery 24. Creation and use of tuples in Python by Manuel Madrigal is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.