Tuples in Python

Beginners Code Computer Skills Python

Understanding Tuples:

Like list, Tuples also belong to the class of standard datatypes. The only problem is that the data becomes immutable once it is stored.

By this, we mean that data addition is flexible until Tuples are created.  However, we can perform some similar operations of a list by extracting parts of a Tuple and creating a new Tuple.

Note that, in Lists, we could modify the values inside the actual List, but here in Tuples, we break it into pieces while performing an operation similar to a List. This concept will get clear as we move on.

Defining Tuples:

A Tuple can consist of values of any data type similar to a List, but they should be enclosed in parenthesis.

The syntax follows as:

sample_tuple = ("mario", "mushrooms", 'a', 1000, 12.5)

Also, an interesting thing in python is, suppose some set of values are written without any delimiters on either side i.e. without enclosing them in square brackets or parenthesis; then python considers them by default to be a tuple. If you want it as a list, then you have to convert it using the appropriate syntax.

Consider the following syntax to understand this:

sample_inputs = "mario", "mushrooms", 'a', 1000, 12.5

Since there is no delimiter, Python will consider this as a Tuple by default.

How can we check that?

Run this code and see what error it gives:

sample_inputs = "mario", "mushrooms", 'a', 1000, 12.5
for i in sample_inputs:
    print(i)

sample_inputs[2]= "coconut"
print(sample_inputs)

We tried to modify one of the sample inputs’ item, but it gave us error showing it is not able to modify it. Hence it shows that by default, they accept any random set of values to be a Tuple.

Accessing values in a Tuple:

The values in a Tuple are accessed in the same way as it was done in the List. The syntax is as follows:

sample_inputs = "mario", "mushrooms", 'a', 1000, 12.5
for i in sample_inputs:
    print(i)

If we want to access a series of values from a Tuple, then we need to mention it inside the square brackets in the following order:

sample_inputs = ("mario", "mushrooms", 'a', 1000, 12.5)
print(sample_inputs[1:3])

Note that it starts printing from the mentioned index but prints till the mentioned last index minus 1.

Basic Operations on Tuples:

Like Lists, we can also perform all the basic operations on a Tuple which includes finding the length of the Tuple, concatenation of two Tuples, Repetition of Tuples, Searching of an element in a Tuple, and Iteration of Tuples.

The following code will make clear all of the following operations:

sample_tuple_1 = ("mario", "mushrooms", 'a', 1000, 12.5)
for i in sample_tuple_1:
    print(i)

# finding the length of a Tuple
length = len(sample_tuple_1)
print(length)

# Concatenation of two tuples
sample_tuple_2 = ("Tony", "Hulk", 3000, 14.05)
concat_tuple = sample_tuple_1 + sample_tuple_2
print(concat_tuple)

# Repetition of Tuples
print(sample_tuple_2 * 4)

# Searching of an Element in an Tuple
for i in sample_tuple_2:
    if i == "Tony":
        print("Tony is alive")
        break

Updating Tuple elements:

As we said, Tuples are immutable, so it is not possible to update the Tuple which you have created. However, you can slice and make new Tuples out of it and thus it might look somewhat similar to updating a Tuple. This can be shown as:

sample_tuple_1 = ("mario", "mushrooms", 'a', 1000, 12.5)
print("The original Tuple is\n")
for i in sample_tuple_1:
    print(i)

sample_tuple_2 = ("Nintendo",)

new_tuple = sample_tuple_1 + sample_tuple_2
print("The modified Tuple is\n")
print(new_tuple)

Deleting Tuple elements:

Since a Tuple is immutable, it is not possible to remove elements from that specific Tuple. However, you can create another Tuple with the undesired elements to make it show that the elements were neglected. It can be shown as:

sample_tuple_1 = ("mario", "mushrooms", 'a', 1000, 12.5)
print("The original Tuple is\n")
for i in sample_tuple_1:
    print(i)

new_tuple = sample_tuple_1[0:4]
print("the new Tuple is\n")
print(new_tuple)

Note that we can still delete the entire Tuple. This can be done by:

sample_tuple_1 = ("mario", "mushrooms", 'a', 1000, 12.5)
print("The original Tuple is\n")
for i in sample_tuple_1:
    print(i)

# this is how we delete the entire tuple.
del sample_tuple_1

#this is to verify if the tuple has been deleted or not.
print(sample_tuple_1)

So what is the benefit of using Tuples?

Usually, in studies like Machine Learning and Artificial Intelligence, we work on huge datasets by filtering very important data from them. Now one might accidentally compose a code which would be changing the predefined values we have obtained. Hence we put them in a Tuple and ensure that they remain unchanged.