A tuple is an ordered sequence that you do not change. Parentheses, or just commas.
Goal
Build tuples, unpack them, and swap two names without a temp variable.
Build
point = (3, 4)
row = "Ada", 98, "A"
print(point)
print(row)
print(point[0], row[-1])
print(len(point))A one-item tuple needs a comma: (3,) not (3).
print(type((3)))
print(type((3,)))Immutable
pair = (10, 20)
# pair[0] = 99 # TypeError — uncomment to see it
print(pair)
print(tuple([1, 2, 3]))
print(list(pair))Unpack and swap
name, score = ("Ada", 98)
print(name, score)
a, b = 1, 2
a, b = b, a
print(a, b)Returning two values
def minmax(values):
return min(values), max(values)
lo, hi = minmax([91, 98, 70, 95])
print(lo, hi)You should see
Functions are a later chapter. You can still run this cell — def is allowed anywhere.