MCS-201 · Unit 10

Data Structures and Control Statements in Python

Section 6 of 12

Block 3 · Unit 10Data Structures and Control Statements in Python5 min · Section 6 of 12

10.6Tuples

Covers IGNOU MCS-201 §10.7.5, 10.8.2

A tuple is like a list that cannot be changed. That restriction is useful: it protects data from accidental change, lets tuples be used as dictionary keys, and makes them the natural way to group a few related values, such as a record or several values returned from a function.

Creating tuples

FormExampleNote
With parenthesest = ('a', 'b', 'c')The usual style
Without parenthesest = 'a', 'b', 'c'The commas make the tuple
One itemt1 = ('a',)The trailing comma is required; ('a') is just the string 'a'
Empty() or tuple()
From an iterabletuple("IGNOU")('I', 'G', 'N', 'O', 'U')

Operations

Tuples support indexing, slicing, +, *, in, len(), min(), max(), sum(), sorted() (returns a list) and comparison. Comparison is lexicographic: elements are compared pair by pair until two differ, so (0, 1, 500000) < (0, 3, 4) is True because 1 < 3. Tuples have only two methods: count(x) and index(x).

Trying to change an item raises TypeError: 'tuple' object does not support item assignment. You can, however, build a new tuple: t = ('A',) + t[1:].

Immutability is shallow

A tuple’s slots cannot be changed, but if a slot holds a mutable object such as a list, that object can still change.

univ = ('du', 'jnu', ignou) where ignou is a list
univ: ( 'du' , 'jnu' , ● )
                       │
                       ▼
                 ['soe', 'soms', 'socis']   <- this list can still grow

Tuple assignment (unpacking)

A tuple (or any sequence) on the right can be unpacked into several names on the left: x, y = ['good', 'luck']. The number of names must equal the number of items. This gives Python’s one-line swap, a, b = b, a: the right side is packed into a tuple first, then unpacked.

Tuples versus lists

ListTuple
Syntax[1, 2](1, 2)
MutableYesNo
MethodsManycount, index
Can be a dictionary key or set itemNoYes, if all its items are immutable
Typical useA collection of similar items that changesA fixed group of related values (a record, coordinates, several return values)
Speed and memorySlightly moreSlightly less

Key takeaways

  • Tuples are ordered and immutable; the comma makes a tuple.
  • They support indexing, slicing, +, *, in, count() and index().
  • Unpacking assigns items to names; a, b = b, a swaps.
  • Use tuples for fixed records and as dictionary keys.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is type(('a'))?

Show answer

Answer: B. str

Without a comma, the parentheses only group an expression; ('a',) would be a tuple.

Question 2 · multiple choice

What does (1, 2, 3)[1:] give?

Show answer

Answer: C. (2, 3)

Slicing a tuple returns a tuple, from index 1 to the end.

Question 3 · multiple choice

What does this program print?

Python · quiz_tuple.py
t = (3, [1, 2], "x")
t[1].append(9)
print(t, len(t))
Show answer

Answer: D. (3, [1, 2, 9], ‘x’) 3

The tuple still holds the same three objects, but the list inside it is mutable and gains 9; the tuple’s length stays 3.

Question 4 · multiple choice

Which can be used as a dictionary key?

Show answer

Answer: B. (1, 2)

Keys must be hashable (immutable all the way through). A tuple containing a list is not hashable.

Question 5 · multiple choice

Which is True?

Show answer

Answer: A. (1, 9) < (2, 0)

Tuples compare item by item: 1 < 2 decides at once. A tuple never equals a list, and a longer tuple with the same start is greater.

Question 6 · short answer

Explain how a, b = b, a swaps two values, and why no temporary variable is needed.

Show worked answer

Python evaluates the right side first, building the tuple (b, a) from the current values. Only then does it unpack the tuple into the names on the left, assigning the first item to a and the second to b. Because both old values are already saved inside the tuple before any assignment happens, neither value is lost, so no separate temporary variable is needed (the tuple acts as one).

↑ ↓ move↵ openTab switch type