⚡0 XP🔥0

Tuples

Lesson 16 of 33 ⏱ 4 min read ✎ 3 exercises ❓ 5-question quiz ▶ Runs Python

Immutable sequences - creating tuples, the single-item trap, packing and unpacking, tuple methods, and when to choose a tuple over a list.

What is a tuple?#

A tuple is an ordered collection, just like a list, but immutable - once created, it cannot be changed. Tuples use round brackets ( ).

point = (10, 20)
rgb = (255, 128, 0)
student = ("Neha", 19, "Bhopal")
empty = ()

Single-item tuples - the comma trap#

▶ Try it YourselfPython
Ctrl + Enter to run
Common Mistake

(5) is not a tuple - it's just the integer 5 with brackets around it. Always add a trailing comma for one item: (5,).

Accessing items#

Indexing, slicing, len(), in and loops work exactly like lists:

▶ Try it YourselfPython
Ctrl + Enter to run

Tuples are immutable#

days[0] = "Monday"   # TypeError: 'tuple' object does not support item assignment
days.append("X")     # AttributeError: 'tuple' object has no attribute 'append'

To "change" a tuple, create a new one:

▶ Try it YourselfPython
Ctrl + Enter to run
हिंदी में समझें

Tuple = list जो बदली नहीं जा सकती (read-only list)।

कब इस्तेमाल करें? जब data ऐसा हो जो कभी नहीं बदलना चाहिए - जैसे हफ्ते के दिन, किसी जगह के coordinates (latitude, longitude), या database का एक record। Tuple इस्तेमाल करने से गलती से data बदल जाने का खतरा नहीं रहता, और यह list से थोड़ा तेज़ और कम memory लेता है।

Packing and unpacking#

Packing: putting several values into one tuple. Unpacking: taking them out into separate variables.

▶ Try it YourselfPython
Ctrl + Enter to run

You already used unpacking when swapping: a, b = b, a - the right side is packed into a tuple, then unpacked.

Returning multiple values from a function#

Functions often return tuples (you'll write functions soon):

▶ Try it YourselfPython
Ctrl + Enter to run

Tuple methods#

Only two, since tuples can't change:

▶ Try it YourselfPython
Ctrl + Enter to run

Tuples as dictionary keys#

Because they're immutable, tuples can be keys in a dictionary (lists can't):

▶ Try it YourselfPython
Ctrl + Enter to run

List vs tuple#

List [ ] Tuple ( )
Mutable? Yes No
Methods many (append, sort...) count, index
Speed / memory slightly slower, more memory slightly faster, less memory
Dictionary key? No Yes
Use for collections that change fixed records, constants, return values

Summary#

  • Tuples are ordered and immutable: (a, b, c).
  • One-item tuple needs a comma: (x,).
  • Indexing, slicing, len, in, loops work like lists.
  • Packing/unpacking: name, age = ("Ravi", 20); first, *rest = t.
  • Only count() and index() methods; can be used as dict keys.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Unpack a record

Unpack the tuple student into name, age and city, then print Neha (19) from Bhopal.

Expected output:
Neha (19) from Bhopal
2

Min and max as a tuple

Create a tuple result containing the smallest and largest values of data (in that order), then print it.

Expected output:
(3, 89)
3

Count a value

Print how many times "A" appears in the tuple grades, and the index of the first "C".

Expected output:
3
2

Quick Quiz

Q1.

Which of these is a tuple with ONE item?

Q2.

What happens with t = (1, 2, 3); t[0] = 10?

Q3.

What does a, b, c = (4, 5, 6) do?

Q4.

Which methods do tuples have?

Q5.

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