In the previous sections of this Mastering Python for Blockchain series, we read about different Data types in Python-Booleans, Integers, Floats and Strings. These Data Types in Python are like atoms. When we combine these Data Types, we get Data Structures in Python. This article focusses on the first Data Structure we will explore in Python-Tuples. There are other Data Structures in Python like lists, sets, dictionaries, which we will learn in the upcoming parts of this Mastering Python for Blockchain Series. If you have not read the previous parts of this Mastering Python for Blockchain Series, please do read them. It will help you build your Python foundation from the beginning. Start from here.

Few quick words: I will suggest you to download the latest version of Python which will help you to practice coding and compiling programs. In case you do not have Python installed in your system you can use this link to online run your Python codes.

I also suggest you to visit the article here to learn the basic concepts of Blockchain.

Data Structures in Python-Tuples and Lists: Definition

In the previous sections, we learnt about strings. Strings are a sequence of characters. Right?

In Python, besides strings, we do have other sequences-Tuples and Lists. These are Data Structures in Python which are basically sequence of zero or more elements. These elements can be any Python objects.

Tuples are immutable whereas Lists are mutable. This means, once you assign elements in a Tuple, it is baked into the cake. The elements cannot be changed. Whereas, the elements in a List can be changed.


This is the first Data Structures in Python, we are getting apprised of. How do you pronounce Tuple? Guido van Rossum, the creator of Python, said via Twitter:
I pronounce tuple too-pull on Mon/Wed/Fri and tub-pull on Tue/Thu/Sat. On Sunday I don’t
talk about them. 🙂


Tuples are created with either a comma, parenthesis or a comma and a parenthesis. It is a bit inconsistent. See below:

Empty Tuple:

empty_tuple=( )

Tuple with a single element

single_element_tuple=’Tuple’, ##see we used a comma
single_element_tuple=(‘Tuple’,) ##we used comma as well as parenthesis.
If we only used parenthesis and no comma in a single element Tuple, it would create a string.

Tuple with multiple elements

multiple_element_tuple=’Element1′, ‘Element2’, ‘Element3’
##follow all but the last one with commas


multiple_element_tuple=(‘Element1’, ‘Element2’, ‘Element3’)

Tuple Unpacking

Tuples can be used to assign values to multiple variables. See below.


In the second line, Python assigns the three variables, land_animal, water_animal and amphibian the elements of Tuple tuple. This assignment occurs in the order of sequence in which the elements appear. This means the first element ‘horse’ is assigned to the first variable land_animal, the second element fish is assigned to the second variable water_animal, and so on. This phenomena is called Tuple Unpacking.

Exchange values of variables

Tuples can be used to exchange the values of variables in a single line. See below.


tuple( ) function

The tuple( ) function is used to create a tuple out of any other data type in Python.


Operators +,* and comparison operators




Iterate with for and in


Another one,

Modify a Tuple

You cannot. Just like strings, Tuple also can’t be modified. It only seems that the tuple is modified. Tuples are immutable.

In the above example, it only appears that the tuple t1 has been modified. In fact, Python created a new tuple t1 from the old tuple t1 by t1+=t1.

You can in fact check the different ids of the two tuples.


Leave a Reply