Skip to lesson
Python for JS Developers

Data StructuresLesson 3 of 3

Tuples & Sets

4 min

Tuples have no real JS equivalent. They are like arrays that cannot be modified after creation. Why would you want that? Immutability makes them safe to use as dictionary keys and set members, and guarantees nobody changes them by accident. Object.freeze() is the closest JS idea, but tuples are a real built-in type with their own literal syntax.

Sets exist in JS too, but Python's are more convenient: they have a literal syntax ({1, 2, 3}), set comprehensions, and math operators built in. Union is |, intersection is &, difference is -, and symmetric difference is ^. JS only gained union() and intersection() methods in ES2025; before that you needed spread and filter tricks.

Tuples are also the default way Python returns multiple values from a function. When you write 'return x, y', Python packs those into a tuple. You can unpack them right back: a, b = get_coords(). This pattern appears everywhere in Python code.

Two rules that catch everyone at least once. First: parentheses do not make a tuple, the COMMA does. (42) is just the number 42 in parentheses. (42,) with a trailing comma is a one-element tuple, and even x = 1, 2 creates a tuple without any parentheses. Second: an empty set is created with set(), not {}. Curly braces alone create an empty dictionary. So {} is a dict, {1, 2} is a set, and set() is an empty set.

Quick reference

JavaScriptPythonNote
Object.freeze([1, 2])(1, 2)Tuple: built-in immutable sequence
new Set([1, 2, 3]){1, 2, 3}Literal syntax with braces
a.union(b) (ES2025)a | bUnion operator
a.intersection(b) (ES2025)a & bIntersection operator
a.difference(b) (ES2025)a - bDifference operator
set.has(x)x in sin operator
return [x, y]return x, yAuto-packs into a tuple

Side by side

JavaScript
// JavaScript
const point = Object.freeze([10, 20]);

const a = new Set([1, 2, 3]);
const b = new Set([2, 3, 4]);
a.add(4);
a.has(3);
// Before ES2025:
new Set([...a, ...b]);           // union
[...a].filter(x => b.has(x));    // intersection
// ES2025:
a.union(b);
a.intersection(b);
Python
# Python Tuples - immutable
point = (10, 20)
x, y = point       # unpacking

def get_user():
    return "Alice", 25   # returns a tuple
name, age = get_user()

# Sets - math operators!
a = {1, 2, 3}
b = {2, 3, 4}
a | b    # Union:        {1, 2, 3, 4}
a & b    # Intersection: {2, 3}
a - b    # Difference:   {1}
a ^ b    # Symmetric:    {1, 4}

evens = {x for x in range(20) if x % 2 == 0}

Exercise

Real Python, running in your browser

Write Python code that does the following:

1. Create a tuple called rgb with values (255, 128, 0) 2. Unpack it into 3 variables: r, g, b 3. Create a set called frontend: "React", "Vue", "Angular", "Svelte" 4. Create a set called backend: "Node", "Django", "Flask", "FastAPI" 5. Create all_tech = union of both sets 6. Print the total count of unique technologies
Expected output
8

Ctrl + Enter runCtrl + Shift + Enter checkTab indentEsc then Tab to leave the editorCtrl + / comment