When you loop
So you are unpacking values to a
Why is it a tuple?
Outside of loop it can be used to make clear that it's a tuple.
for name, species, weight in pets:
is equivalent to name, species, weight = pets[0]
loop name, species, weight = pets[1]
loop ...ect.So you are unpacking values to a
tuple
name, species, weight.Why is it a tuple?
>>> pets = [["Garfield", "cat", 4], ... ["Dog", "dog", 7.5], ... ["Porkey", "pig", 61], ... ["Stoned", "rock", 0.230] ] >>> name, species, weight = pets[0] >>> name, species, weight ('Garfield', 'cat', 4) >>> # Or to make it clear >>> 'a', 'b', 'c' ('a', 'b', 'c') >>> name 'Garfield' >>> species 'cat' >>> weight 4So can use
() or []
,but it's not needed and in a loop is just confusing.Outside of loop it can be used to make clear that it's a tuple.
>>> tup = 1, 'a' >>> tup (1, 'a') >>> tup1 = (1, 'a') >>> tup1 (1, 'a') >>> tup == tup1 True