Excellent.
DAY 10 is where Python’s hash-based thinking becomes crystal clear.
If you truly understand today, you’ll:
- Instantly reason about
set,dict, membership tests - Understand why order is not a promise
- Never be confused by
{1, True, 1.0}again - Speak confidently about hash tables in interviews
🧠 DAY 10 — Sets & Frozensets
(Hash Tables, Collisions & Mathematical Semantics)
🔑 CORE IDEA OF DAY 10
A set is an unordered collection of unique, hashable objects implemented using a hash table.
Everything today flows from:
- Hashing
- Equality
- Immutability
1️⃣ What a Python Set REALLY Is
A Python set is:
- A hash table
- Stores only keys (no values)
- Requires elements to be hashable
- Optimized for membership testing
s = {1, 2, 3}
Internally:
set
├── table (array of slots)
├── hash → index
└── open addressing (probing)
2️⃣ Visual: Set Internal Structure



Each element:
- Computes
hash(x) - Maps to a slot
- Probes if collision occurs
3️⃣ Why Sets Are FAST (O(1) Average)
Membership test:
x in s
This is:
- O(1) average
- Independent of set size
Compared to list:
x in lst # O(n)
This is why sets dominate:
- Deduplication
- Lookups
- Graph algorithms
- Join-like logic
4️⃣ Hashing Rules (CRITICAL)
For any object used in a set:
- Must implement
__hash__ - Must implement
__eq__ - If
a == b→hash(a) == hash(b)
Python enforces this contract.
5️⃣ The Legendary {1, True, 1.0} Trap 🔥
s = {1, True, 1.0}
print(s)
✔ Output:
{1}
WHY?
| Value | == 1 | hash() |
|---|---|---|
1 | True | same |
True | True | same |
1.0 | True | same |
So:
- All equal
- All same hash
- Set keeps only one
🧠 Sets care about equality, not type.
6️⃣ Why Sets Are Unordered (IMPORTANT)
A set:
- Does not preserve insertion order
- Stores elements based on hash & probing
- Order may change across runs / versions
print({10, 20, 30})
Never rely on order.
⚠️ Dicts preserve insertion order
⚠️ Sets do NOT promise it
7️⃣ Mutability Rules (Why Lists Can’t Be in Sets)
s = {[1, 2]}
❌ TypeError: unhashable type: 'list'
Why?
- Lists are mutable
- Hash would change
- Set invariants would break
But:
s = {(1, 2)}
✔ Works (tuple is immutable)
8️⃣ frozenset: The Immutable Set
frozenset:
- Immutable version of set
- Hashable
- Can be dict keys or set elements
fs = frozenset([1, 2, 3])
Use cases:
- Nested sets
- Graph edges
- Caching keys
9️⃣ Set Operations = Mathematical Set Theory
Python sets support:
A | B # union
A & B # intersection
A - B # difference
A ^ B # symmetric difference
These operations:
- Are implemented in C
- Use hash tables efficiently
- Often faster than manual loops
🔟 Collision Handling (Internals, High Level)
Python uses:
- Open addressing
- Probing sequence
- Resizing when load factor increases
You don’t control collisions — Python handles them.
11️⃣ Performance Characteristics Summary
| Operation | Avg Time |
|---|---|
| Add | O(1) |
| Remove | O(1) |
| Membership | O(1) |
| Iteration | O(n) |
Worst-case exists but rare.
🔥 INTERVIEW TRAPS (DAY 10)
Q1
print(len({True, 1, 1.0, False, 0}))
✔ 2
(One for truthy-1, one for falsy-0)
Q2
a = {1, 2, 3}
b = a
a.add(4)
print(b)
✔ {1, 2, 3, 4}
(Mutation)
Q3
print(set("hello"))
✔ {'h', 'e', 'l', 'o'}
(Order undefined)
Q4
Why is set faster than list for lookups?
✔ Hash table
✔ O(1) average
✔ No scanning
🧠 DAY 10 MENTAL CHECKLIST
Before using a set, ask:
- Do I need uniqueness?
- Do I need fast lookup?
- Are elements immutable?
- Is order irrelevant?
If YES → set / frozenset
📝 DAY 10 ASSIGNMENT (MANDATORY)
1️⃣ Predict output (NO RUNNING):
s = set()
s.add(1)
s.add(True)
s.add(1.0)
s.add(False)
print(s)
2️⃣ Explain clearly:
- Why sets cannot contain mutable objects
- Why equality matters more than type in sets
3️⃣ Design question:
Why did Python designers choose hash tables over trees for sets?
🔜 DAY 11 PREVIEW (🔥🔥🔥)
DAY 11 — Dictionaries (CPython Hash Table Internals & Order Preservation)
This is the most important built-in structure in Python.
You’ll learn:
- Dict internals
- Why dicts are insanely fast
- Order preservation (Python 3.7+)
- Real interview traps with dict keys
When ready, say 👉 “START DAY 11”