Tuples

A tuple is an ordered collection like a list — but frozen . Once you create it, it can never change. That sounds restrictive, yet it's exactly what you want for fixed records: a coordinate, an RGB color, a database row, a function returning two values at once.

Learn Tuples in our free Python course — a beginner-friendly interactive lesson with runnable examples, a practice exercise and a quick recall.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Tuples also unlock Python's elegant unpacking syntax, which you'll use constantly.

Tuples use parentheses (or just commas). Once built, they're locked:

You can read a tuple freely, but any attempt to change it raises an error. That guarantee is exactly why tuples are useful for fixed data:

Unpacking spreads a tuple's elements into separate variables in one clean line. You've already seen it in variable swapping — it's everywhere in Python:

This is the single most common professional use of tuples. A function that needs to hand back two or three results just returns them comma-separated — Python packs them into a tuple, and the caller unpacks:

Looping over pairs uses the same idea — enumerate() and zip() (next lessons) both yield tuples that you unpack right in the for statement.

Everything you can do to read a list, you can do to a tuple. Only the editing operations are missing:

Tuples have just two methods ( count and index ) because they can't be modified — there's no append , sort , or remove . To "change" a tuple, convert it: list(my_tuple) , edit, then tuple(...) back.

These lines should compute a midpoint from two coordinate tuples and print it. Reorder them so the output is Midpoint: (3.0, 4.0) .

Why: the tuples a (B) and b (E) must exist before you unpack them (D, A). The midpoint calculation (C) needs all four coordinates, and the print (F) comes last.

<class 'int'> — without a trailing comma, parentheses just group a single value. (7,) would be a tuple.

[2, 3, 4] — a takes the first item and the starred b collects the rest into a list.

TypeError — tuples are immutable, so item assignment raises "'tuple' object does not support item assignment". Nothing prints.

Use tuple unpacking to compute the distance between two points.

Lesson complete — tuples mastered!

You can create tuples (including the tricky single-item case), use their immutability deliberately, unpack them into variables, and return multiple values from functions. Tuples are the quiet workhorse behind much of Python's clean syntax.

🚀 Up next: Comprehensions — build lists, dicts, and sets in a single expressive line.

Practice quiz

Which brackets are conventionally used to create a tuple?

Tuples use parentheses, e.g. (3, 4), though the commas are what truly create them.

What is type((5))?

  • int
  • tuple
  • list
  • set

Answer: int. Without a trailing comma, (5) is just the integer 5 in parentheses.

How do you create a single-item tuple containing 5?

  • (5)

A one-element tuple needs a trailing comma: (5,).

What makes a tuple different from a list?

  • Tuples are unordered
  • Tuples are immutable (cannot be changed)
  • Tuples can't hold strings
  • Tuples can't be looped

Answer: Tuples are immutable (cannot be changed). Tuples are immutable: once created their elements cannot change.

What does t[0] = 9 do for a tuple t = (1, 2, 3)?

  • Raises a TypeError
  • Changes the first item to 9
  • Returns 9
  • Adds 9 to the tuple

Answer: Raises a TypeError. Item assignment on a tuple raises a TypeError because tuples are immutable.

After first, *others = (1, 2, 3, 4, 5), what is others?

  • 1
  • (2, 3, 4, 5)

The starred variable collects the remaining items into a list: [2, 3, 4, 5].

Why can a tuple be used as a dictionary key but a list cannot?

  • Tuples are shorter
  • Tuples are hashable (immutable)
  • Lists are too slow
  • Tuples are ordered

Answer: Tuples are hashable (immutable). Tuples are hashable because they're immutable, so they can be dict keys or set members.

Which two methods do tuples have?

  • append and sort
  • add and remove
  • push and pop
  • count and index

Answer: count and index. Tuples only have count and index because they can't be modified.

What does divmod(17, 5) return?

  • 3.4
  • (3, 2)

Answer: (3, 2). divmod returns a tuple of the quotient and remainder: (3, 2).

A function with 'return min(nums), max(nums)' returns what kind of object?

  • A list
  • Two separate values
  • A tuple
  • A dictionary

Answer: A tuple. Comma-separated return values are packed into a single tuple.