INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
LESSON 14
PAGE 1 / 4
Tuples
Home >>> Online Lessons, Python 3

What is a Tuple?

Suppose we want to process data about multiple people and for each, we know their name, surname, age, height, city, and county.



We could create 6 lists, one for each piece of information, but as you might have noticed, if we alter (i.e., modify in some way) one of the lists, the indices no longer match, and the positions can no longer be accessed correctly. What can we do more easily instead? We can use tuples!

A tuple is also a kind of list, but its elements are ordered and immutable, and they are defined using round brackets:
Editor - lesson_14.py
       
Console/Output done
DETAILS

Above, we defined two tuples, in our case for Fox and Dana from the X files series, let's say:



Each element of the tuples stores a specific piece of information that we have appropriately entered, so as to obtain a coherent data structure.

We know that the created tuples store the age at index 2, and the county at 5, etc. Thus, we have a consistent mechanism for any other person, and we will create a tuple for each one.

Note: For those who have studied C++, tuples are similar to the struct type.

We cannot add, delete, or modify elements defined in a tuple! Any such operation will give us a runtime error:



But what can we do instead?
Proceed to the next page.
 home   list  CONTENTS   perm_identity   arrow_upward