kota's memex

In python a tuple is a bit like a list, but they are immutable.

In other languages the distinction is usually that tuples can contain elements of differing types, but in python a normal list does not have a type restriction.

dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])

Commas for a single item tuple:

# Note that a tuple of length one has to have a comma after the last element but
# tuples of other lengths, even zero, do not.
type((1))   # => <class 'int'>
type((1,))  # => <class 'tuple'>
type(())    # => <class 'tuple'>

Although the underlying tuple is immutable, you can change the variable to point to a different tuple like so:

dimensions = (200, 50)
dimensions = (400, 100)

unpacking

You can unpack tuples (or lists) into variables

a, b, c = (1, 2, 3)  # a is now 1, b is now 2 and c is now 3
a, *b, c = (1, 2, 3, 4)  # a is now 1, b is now [2, 3] and c is now 4

# Now look how easy it is to swap two values
e, d = d, e  # d is now 5 and e is now 4