Tuples in Python - Scaler Topics (original) (raw)

Tuples in python is one of the four inbuilt data types used to store collections in Python. Unlike other data types, the elements in tuples are ordered and immutable. They are used to store multiple items in a single variable and provides some built-in operation to work with them.

Creating a Tuple in Python

Tuples in Python can only be created when they are being assigned, hence placing all the elements inside the parenthesis, separated by a comma will create a tuple. Let’s take a closer look at the syntax:

Output

The parenthesis in the above syntax are optional and hence a tuple can also be created without writing the parenthesis. But remember it’s always a good practice to have the parenthesis, as it always increases the code readability and understanding.

The above snippet can also be written as:

Output

Immutable in Tuples

Tuples in Python are similar to lists but with a key difference: they are immutable. This means once a tuple is created, its elements cannot be changed, added, or removed. Let's break down some characteristics of tuples:

When attempting to modify an element within a tuple, Python will raise a TypeError because tuples in python do not support item assignment due to their immutable nature. This constraint ensures that once data is stored in a tuple, it remains unchanged throughout the program's execution.

Python Tuple Types

Python offers two primary types of tuples: named tuples and unnamed tuples.

Outputs

Outputs

In this instance, a tuple in python containing three elements is created. Accessing elements within the tuple can be achieved using index notation, akin to how one would interact with a list.

Accessing Elements in a Python Tuple and Indexing

Accessing elements in a tuple is no different then accessing elements in a list. As python follows 0 based indexing hence a tuple with n elements will have indices from 0 through n-1. An index in a tuple is accessed with the index operator [ ]. For example:

Let’s consider a basic tuple:

Nested Python Tuple Accessibility

Accessing Via Negative Indices

Python allows you to access elements of a collection via negative indices. When accessing using a negative index, -1 depicts the last element and -n depicts the first index where n is the length of the index.

Consider the following mapping of positive index with negative index:

accessing tuples via negative indices

Updating Tuples in Python

Adding a new element or deleting one is not really an option when dealing with tuples in python, as they are immutable. Even the elements of the tuple cannot be updated until and unless the element is mutable for example a list.
Let’s take an example

Output

Tuples in python can definitely be reassigned, which is very different from updating a tuple. Reassigning a tuple is redefining the tuple all over again.

Just like strings, we can also concat two or more tuples to form a new tuple using ‘+’ operation or apply repetition on a tuple using ‘*’ operator, just the result here is a python tuple and not a string.

Output

In-built Functions for Tuple

Python has the following inbuilt functions to offer for tuples:slicing in python tuples

tuple[start : stop : step]

Consider the above figure when understanding the following code snippet

Output

Output

Output:

Advantages and Disadvantages of Tuple in Python

Advantages

Disadvantages

Tuple’s advantages and disadvantages are nothing but its use cases i.e. tuple serves some use cases hence one should know when to use tuple, in order to use it for their advantage. Tuples when used where a list or a dictionary or a set would’ve been used, will turn out to be a disadvantage.

Conclusion

  1. Tuples support integer-based indexing and duplicate elements, enhancing data organization and retrieval.
  2. They can be defined with or without parentheses; however, a trailing comma is necessary without parentheses to signify a tuple.
  3. Optimal use of tuples depends on their intended application; misapplication can lead to inefficiencies, such as substituting for lists, sets, or dictionaries.
  4. Choosing the appropriate data structure requires careful consideration of use cases to ensure efficient data handling and manipulation.