Python 3D Academyবাংলা

Tuples

🌍 In real life. Your date of birth is three values that belong together: day, month, year. A map location is two: latitude and longitude. You never "append" a fourth value to a date, and you'd never want someone to change just the month by mistake. They're a fixed bundle.

For bundles like that, Python has the tuple.

The idea in one sentence: a tuple is like a list written with round brackets ( ), but it can't be changed once it's made.

Its words:

  • tuple: (3, 4): values in order, fixed
  • immutable: can't be changed after it's made (tuples and strings are; lists aren't)
  • (5,): a tuple with one item needs the comma

Step by step, for point = (3, 4):

  1. Python makes a tuple holding 3 and 4, in that order.
  2. point[0] is 3 and point[1] is 4, just like a list.
  3. point[0] = 9 is refused: a tuple can be read, never changed.
point = (3, 4)
print(point)
print(point[0], point[1])
print(len(point))
Indexes, slices, len() and in all work on tuples, just like on lists.

Trying to change a tuple stops the program with a TypeError. There's no append or remove either.

point = (3, 4)
point[0] = 9

A tuple with just one item needs a comma: (5,). Without the comma, (5) is just the number 5 in brackets. You can also turn a list into a tuple with tuple(), and a tuple into a list with list().

print(type((5,)))
print(type((5)))
print(tuple([1, 2, 3]))
print(list((1, 2, 3)))

Tuples often sit inside lists, one tuple per thing:

points = [(1, 2), (3, 4), (5, 0)]
for p in points:
    print("x =", p[0], "and y =", p[1])

Why have tuples at all? Because "can't change" is a promise. When you pass a tuple around your program, nothing can quietly alter it, so a point stays the same point. That promise also lets a tuple be used where Python needs values that never change, such as a dictionary key (Chapter 11): {(1, 2): "a"} works, while a list as a key doesn't. Reading a tuple costs the same as reading a list: indexing is instant, and in checks item by item.

Mistakes and edge cases.

  • Changing an item. point[0] = 9 gives TypeError: 'tuple' object does not support item assignment (above).
  • List methods. point.append(5) gives AttributeError: 'tuple' object has no attribute 'append'. To "change" a tuple, build a new one.
  • One item without a comma. (5) is just the number 5: len((5)) gives TypeError: object of type 'int' has no len().
  • Forgetting the brackets are optional. point = 3, 4 also makes a tuple: it's the comma that matters.

Quiz

  1. What does this print?

    t = (1, 2, 3)
    print(t[-1])
  2. What happens when you run t[0] = 5 on a tuple t?

    • A TypeError: tuples can't be changed
    • The first item becomes 5
    • 5 is added to the front
  3. What does this print?

    print(type((7,)))
  4. What does this print?

    pair = ("Ana", 9)
    print(pair[0], "is", pair[1])
  5. Which line has a mistake?

    days = ("Mon", "Tue")
    days.append("Wed")
    print(days)

Coding tasks

  1. Make a tuple called rgb with the numbers 255, 128 and 0, in that order. Then print the middle number.

  2. Ask for whole numbers on one line, separated by spaces. Put them in a tuple of numbers and print it.

  3. Here are some points: points = [(1, 2), (3, 4), (5, 0)]. For each point, print x + y on its own line.