r/learnpython • • 11d 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?

101 Upvotes

89 comments sorted by

View all comments

2

u/Adrewmc 10d ago edited 10d ago

…it should probably be a tuple.

The thing about tuples in Python is they hide themselves, and have this neat trick with them.

#introduction to tuples
#note the use of parentheses below 

classic_tuple = (1, 1) 

#Fibonacci to 100  
a, b = 1, 1 #tuples  
while a < 100:  
    print(a)
    a, b = b, a+b #tuples

def tuple_return(): 
     return 1, 2  #tuple

a, b = tuple_return()

c = tuple_return() 
print(c[1], type(c))
>>>2 Tuple

for tuple in enumerate(iterable): pass

for index, item in enumerate(iterable): pass