Beyond Lists: Exploring the Untapped Potential of Python Tuples

Sayali Kumbhar
4 min readMay 6, 2024

--

Hey Folks 👋,

Let’s talk about tuples. but Before starting the next chapter if you are new to Python Programming and want to learn Python Programming then follow me. If you haven’t read my previous blogs click here to read about the basic guide to Python Program. Now let’s dive into our tuples. for understanding tuples prerequisites that is basic data types and, basics of Python, if you are a new learner click here to start learning Python. Now we are ready to jump on our new journey🤩. so let’s go…

What is tuple?🤔

let’s Understand this with simple words. tuple is a sequence data type. tuple is a collection of objects (collection of different data types). tuples are similar to lists but the main difference is tuples are immutable whereas lists are mutable. tuples are ordered and allowed to store duplicate values.

But hold on what is immutable?

in lists, we can modify lists by applying addition, and deletion. but in the case of tuples, we cannot add, or delete once the element is added.

tuples are declared by round brackets(“ () ”).

following are some characteristics of tuples :

  1. we can search elements from the tuple as it doesn’t allow any changes.
  2. once the tuple is created we cannot add an element to it.
  3. tuples cannot extended
  4. we cannot delete an element from the tuple

let’s understand tuples through the program

in the above code, we have created a tuple mytuple and printed tuple elements.

see we can access elements from the tuple same like accessing elements from lists, using indexing.

as the above image shows tuples are immutable, as I am trying to add a new value of 10 at index 2 then it is giving me an error.

for accessing values from tuples we can use negative indexing too. let’s understand this through the program.

we have seen what we cannot do with a tuple, then what we can do with a tuple?

Concatenation

concatenation means adding two tuples.

but wait how do we find a length of tuple?

the answer is simple using len() function in Python.

what if I want to add tuple inside tuple? let’s do that

simply create the tuples which you want to add create a third tuple and add that tuple to the final tuple. simple isn’t it 🤩. adding tuples inside tuples is called nesting.

now we have created a tuple but I want the first three elements from the tuple then how do we do?

in the above code, I have printed the first 3 elements, the method I have used is called slicing, if you want to learn in-depth about slicing click here.

we have seen some operations but what if I want to delete the tuple?

for deleting the tuple we can use the del keyword.

I have shown some operations with tuples, but where actually we can use tuples?

tuples are used where we want to store data that should not be changed, like in configuration settings, to store coordinates in 2D.

Conclusion

We have seen various Operations which we apply on tuples, and we have seen characteristics of tuples. tuples are used where the task is to store unchanged values like coordinates in 2D. In this blog, I have covered a few operations which are used while working with tuples. As tuples are immutable accessing elements from tuples is faster than lists. Next, I will talk about the remaining sequence types like sets, and dictionaries, and how they are different from each other.

--

--

Sayali Kumbhar

Python Enthusiast 🐍 | Machine Learning Enthusiast | Data Science Enthusiast