Lesson 9Understanding Basic Data Structures: Tuples
In this lesson, we continue to look at understanding basic data types in Python; however, we shift from lists to tuples. In the previous lesson, you learned about lists, which store a sequence of data values. You learned that these values stored in a list did not have to be unique and that you could add and remove them. In this lesson, the focus shifts to tuples, which are a collection of values, which will still allow you to have duplicates; however, the values are ordered and unchangeable.
TUPLES AND TUPLE OPERATIONS
A tuple is another way to save a group of related pieces of data in Python. The biggest difference between a tuple and a list is that a tuple is immutable, which means that we cannot add or remove items from a tuple after we have defined it. However, as with lists, we can:
- Store values of any data type, including different data types in the same tuple
- Compute the length of a tuple
- Use index values to retrieve specific items from a tuple
- Slice tuples to retrieve multiple items at the same time
The basic syntax of a tuple is:
thisTuple = ("element1", "element2", "element3")
Syntax of Tuples vs. Syntax of Lists
Like lists, tuples are a basic data structure type in Python, and ...
Get Job Ready Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.