Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Tuples & Sets

12 min 25 XP
Study loop Read Predict Run Tweak Prove Review
Lesson 4 of 6 · View course roadmap
Step 1

Learn the idea

Tuples are immutable lists — once created, they cannot change: point = (3, 5). Use them for fixed groups of values. Tuple unpacking is beautiful Python: x, y = point, and swapping is just a, b = b, a.

Sets hold unique values with no order: tags = {"python", "web"}. Adding a duplicate does nothing. Sets are perfect for:

  • De-duplicating: set([1, 2, 2, 3]){1, 2, 3}
  • Lightning-fast membership tests: x in my_set
  • Set math: a & b (intersection), a | b (union), a - b (difference)

Where you'll use this

Functions that 'return two values' actually return one tuple — divmod(17, 5) gives (3, 2). Sets power tag systems, deduplicating email lists, and 'which users are in group A but not B' queries.

Common mistakes

  • A one-item tuple needs a comma: (5) is just the number 5; (5,) is a tuple.
  • {} creates an empty dict, not a set — empty set is set().
  • Sets are unordered: never rely on the order you see when printing one.

Pro tip

Tuple unpacking works in loop headers: for name, score in pairs: — and the swap idiom a, b = b, a needs no temp variable. Interviewers notice when you use it.

Step 2

Try it yourself

Blank · autosaved

The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.

PYexample.py
+ Enter to run
Output appears here…
Step 3

Pass the challenge +25 XP

Blank · autosaved

The starter has a list with duplicates. Print the number of unique values in it, then print whether 7 is one of them (True/False).

Target output
4
True
PYchallenge.py
Run your code to check it…
Step 4

Check your understanding

1. What happens if you try point[0] = 9 on a tuple?
2. What is {1, 2, 3} & {2, 3, 4}?
Last step

Your notes (saved on this device)

Tip: use and to move between lessons, K to search everything.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path