man in black long sleeve shirt wearing black headphones sitting on chair
Photo by Nubelson Fernandes on Unsplash

Tuples represent an immutable data structure that can be used to store an ordered sequence of data.

Once you initialize them, you cannot change their values.

Let’s see an example of a tuple:

Since it can be quite challenging to keep track of each of the elements in a tuple, we can also use named tuples.

To do that, we are going to use namedtuple function from the collections library.

Let’s say that we need to declare a day as a tuple that has 2 attributes.

We can use more than just 2 attributes, but let’s keep it simple for the time being.

Now, when we want to declare new tuples, we can use Day like the following:

With these, we can access the name of the day and the index of the day just like we would access attributes of a class:

Yes, it’s that simple.

Here is the complete example that you can copy-paste on your favorite editor and try it yourself:

Thanks for taking the time to read this article. I hope you find it useful.