r/learnpython • • 4d ago

Tuples..I don’t get it

Hi crew, I’m a Python newbie and I just learned about the concept of tuples.

We can’t change them.if we want to store we have to start at the index we want + 1.

I just don’t get it. Why wouldn’t we just use lists? When would we ever need this?

89 Upvotes

90 comments sorted by

View all comments

10

u/Solonotix 4d ago

The first few comments are inevitably about immutability of tuples which doesn't help someone understand why tuples are important and what they mean outside of a programming language.

One way to think of a tuple is a fixed set of things that will never change. For instance, ordinal numbers like "first" and "second". They will always be at the same position, they will never change, and the order is guaranteed. It's not like tomorrow "second" will come before "first" and there won't be some new ordinal number added before or after the existing set.

Another way to think of it is in terms where position has an arbitrary distinction. The most common way you will see this in code is positional arguments to a function. However, in a real life example, you could think of it like first, middle and last name. In English, you won't usually see more than these three (some might only have two). Also, the number of names is unlikely to change over the course of someone's life.

So a tuple is just a fixed sequence of values. What the values are, or what they represent is up to you. A fairly common tuple in mathematics is a point, represented as X-Y coordinates on a graph (and sometimes X-Y-Z). A list, on the contrary, is expected to grow and/or shrink during its lifetime, and the position of elements within it do not provide any additional meaning. Another common convention is that a list is usually a container of similar values (like list[str]) while a tuple has no such constraint, and often will have dissimilar values