📘 CodingMarble Learn

Tuples in Python

A tuple is an ordered, immutable sequence written in round brackets: T = (4, 7, 2). Items are read by index but cannot be changed, added or removed. A single-item tuple needs a trailing comma: (5,). Operations: + (join), * (repeat), in / not in, slicing and comparison. Functions and methods: len(), tuple(), count(), index(), sorted(), min(), max(), sum(). Tuple assignment unpacks values into variables in order (a, b = 3, 8), which also swaps values in one line (a, b = b, a). A nested tuple holds tuples, useful for fixed records such as (roll, name, marks).

🎬 Step-by-step story

  1. A tuple keeps values in order inside round brackets. You can read them, but you cannot change them.
  2. Tuples can be joined, repeated, checked with 'in' and sliced. One item needs a comma: (5,).
  3. Functions like len, count, index, min, max, sum and sorted work on tuples.
  4. Tuple assignment unpacks values into names in order. a, b = b, a swaps two values.
  5. A nested tuple is a tuple of tuples, like rows of student records.
  6. Your turn: type some values and see what the tuple functions give.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why have tuples if lists can do everything?

Tuples protect data from accidental change and are slightly faster. Some things, like dictionary keys, must be immutable, so tuples work but lists do not.

Why is (5) not a tuple?

Brackets are also used for grouping in maths, so Python needs the comma to know you mean a tuple.

If tuples can't change, how does T + (5,) work?

It builds a brand-new tuple. The old T is untouched.

How does a, b = b, a swap without a third variable?

Python first packs the right side into a temporary tuple (b, a), then unpacks it into a and b. Watch the two blocks trade places.

How do I loop through records in a nested tuple?

Use for r in S and read r[0], r[1]…, or unpack directly: for roll, name, m in S.

Creating and accessing tuples

T = (4, 7, 2), E = (), S = (5,) (comma needed), P = 1, 2, 3 (brackets optional), tuple('ab') → ('a', 'b').

Read by index: T[0] = 4, T[-1] = 2. Tuples are immutable: T[0] = 5 gives TypeError. (If a tuple holds a list, that list itself can still change.)

Tuple operations

Each operation makes a new tuple; the old one is unchanged.

Tuple functions and methods

Function / methodT = (4, 7, 2, 7)
len(T)4
T.count(7)2
T.index(2)2
min(T), max(T), sum(T)2, 7, 20
sorted(T)[2, 4, 7, 7] (a list)
tuple(list)converts a list to a tuple

There is no append, remove or sort method, because tuples cannot change.

Tuple assignment

Packing: t = 3, 8 puts values into one tuple. Unpacking: a, b = t gives a = 3, b = 8. The number of names must equal the number of values, or you get ValueError.

Swapping without a third variable: a, b = b, a. The right side is packed first, then unpacked into a and b.

A function can return several values as a tuple: q, r = divmod(17, 5) → 3, 2.

Nested tuples

S = ((1, 'Riya', 91), (2, 'Aman', 85))
print(S[1][1])          # Aman
for roll, name, m in S:
    print(name, m)

Nested tuples store fixed tables, like records that should not change by mistake.

List vs tuple

ListTuple
[ ]( )
MutableImmutable
Many methods (append, sort…)Only count() and index()
For data that changesFor fixed data; a bit faster and safer

Key formulas and definitions

Worked examples

1. T = (10, 20, 30, 40). Find T[1:3], T[-1], T + (50,), 25 in T.

(20, 30); 40; (10, 20, 30, 40, 50); False.

2. What is type((5)) and type((5,))?

type((5)) is int (brackets only group); type((5,)) is tuple.

3. Output? T = (3, 1, 3, 2); print(T.count(3), T.index(2), sorted(T))

2 3 [1, 2, 3, 3]

4. Swap x = 5 and y = 9 using a tuple.

x, y = y, x → x = 9, y = 5.

5. Find max and min in a tuple of marks entered by the user.

T = tuple(int(x) for x in input().split()) # or build a list then tuple() print(max(T), min(T))

6. S = ((1, 'Riya', 91), (2, 'Aman', 85)). Print the average marks.

total = 0 for r in S: total += r[2] print(total / len(S)) # 88.0

Common mistakes

Practice quiz

1. Which creates a tuple with one item?
2. Tuples are:
3. T = (1, 2, 2, 3); T.count(2) is:
4. a, b = 4, 9; a, b = b, a. Now a is:
5. Which method does a tuple have?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is a tuple in Python?

An ordered, immutable sequence of values written in round brackets, like (4, 7, 2).

What is tuple assignment?

Assigning the values of a tuple to several variables at once in order, e.g. a, b = 3, 8; it also swaps values with a, b = b, a.

What is the difference between a list and a tuple?

A list is mutable and uses [ ]; a tuple is immutable, uses ( ) and has only count() and index() methods.

Where this is taught

CBSE (India)Class 11Computational Thinking and Programming - 1

Learn first

Learn next

Related lessons

All Computer Science lessons