Tuples in Python: The Key to Optimized Data Structures in Python
You may have learned all about Lists in python in the previous modules, and in this one, you are going to learn an advanced version of it, Tuples.
Tuples are similar to Lists but their differences lie in their characteristics for example, one of the key differences between them is that tuples are immutable, meaning that the elements cannot be altered once the tuple is created. This makes Tuple a bit more advanced than Lists and let us understand more about it in this article.
Understanding Tuples:
A tuple is an ordered collection of elements in Python. It is a data structure that allows us to store multiple items together.
Whenever you want to represent a fixed set of values or when you need to return multiple values from a function, tuples are the best choice as they provide a convenient way to group data and they can also serve as a key in dictionaries.
Syntax of Tuple:
In Python, you can create a tuple using the following syntax:
>>> my_tuple = (element1, element2, element3, ...)
Here's a breakdown of the syntax:
- Parentheses: Tuples are typically defined using parentheses (). The elements of the tuple are enclosed within these parentheses.
- Elements: The elements of a tuple are comma-separated values. You can include any number of elements in a tuple, and they can be of different data types.
You can also create a tuple without using parentheses by simply separating the elements with commas:
>>> my_tuple = 1, 2, 3, 4
You can use the tuple() function to create a tuple from an iterable, such as a list:
>>> my_list = [1, 2, 3]
>>> my_tuple = tuple(my_list)
If you want to create a tuple with a single element, you need to include a trailing comma after the element.
>>> single_tuple = (42,)
The trailing comma is important because, without that, it would be interpreted as a value rather than a tuple.
Characteristics of Tuples:
Let us see some key characteristics of Tuples:
- Immutable: Tuples in Python are immutable which prevents us from adding, removing, or changing elements that are within the tuple once it has been created.
- Ordered Collection: The elements in a tuple have a specific order or sequence and the order in which they are added is preserved.
- Indexing: Elements within a tuple can be accessed using zero-based indexing. Each element in the tuple is assigned a unique index based on its position. You can use square brackets [] along with the index number to access a specific element.
>>> my_tuple = (1, 2, 3)
>>> print(my_tuple[0])
1
- Length and Count: The length of a tuple refers to the number of elements it contains. You can determine the length of a tuple using the built-in len() function. You can also count the occurrences of a specific element within a tuple using the count() method.
>>> my_tuple = (1, 2, 3, 3, 2, 1)
>>> print(len(my_tuple))
6
- Heterogenous: Tuples can store elements of different data types, that is, they can have integers, strings, floats, and so on.
>>> my_tuple = ("John", 25, 3.14, True)
- Packing and Unpacking: Tuples support packing and unpacking. Packing is the process of combining multiple values into a single tuple while unpacking involves assigning the elements of a tuple to individual variables.
>>> my_tuple = 1, 2, 3
>>> a, b, c = my_tuple
>>> print(a, b, c)
1 2 3
- Grouping of Data: Tuples have the ability to group related data in a set and pass it around as a single entity.
The concept of tuples in Python combines the characteristics of being ordered, immutable, and allowing heterogeneous elements, providing a versatile data structure for various programming scenarios.
Concluding Thoughts
In this article, you learned all about tuples, their introduction, their syntax along with their characteristics. I hope you understood the concept thoroughly and now have a steady grasp of it. I'll meet you with another interesting concept.