Python Learning Series · @onlysamarrr · Day 19 / 30

Day 19: Sets

Day 19 teaches sets. A set holds only unique values: duplicates are removed automatically. Union (|), intersection (&), difference (-) and symmetric difference (^) compare sets.

Phase: Intermediate · Reels: 91/450 – 95/450

Quick Answer

Day 19 teaches sets. A set holds only unique values: duplicates are removed automatically. Union (|), intersection (&), difference (-) and symmetric difference (^) compare sets.

What You Will Learn

Code

a = {1, 2, 3}
b = {2, 3, 4}
print(a | b)
print(a & b)
print(a - b)
print(a ^ b)
visits = ["u1", "u2", "u1", "u3"]
print(len(set(visits)))
print(set())

Output

{1, 2, 3, 4}
{2, 3}
{1}
{1, 4}
3
set()

Code Explained, Line by Line

  1. {1, 2, 3} creates a set inside curly brackets. An empty set must be written as set().
  2. a | b is the union: all unique items from both sets.
  3. a & b is the intersection: only items present in both sets.
  4. a - b is the difference: items only in a.
  5. set(visits) removes duplicate visits, which is how you count unique visitors.
Common Mistake: {} is an empty dict, not an empty set. For an empty set, write set().

Real-Life Use

Unique visitors, common friends and duplicate removal all use sets.

Practice Task: Find the common elements of two lists using set intersection, and count unique visitors.

Test Yourself — MCQs

Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.

Q1. What is {1, 2} | {2, 3}?

Q2. What is {1, 2} & {2, 3}?

Q3. What is {} in Python?

Today's 5 Reels (from the 90-Day Plan Excel)

91/450ConceptSet kya hai
92/450Live demoUnion, intersection demo
93/450Common mistake{} empty set nahi hai
94/450QuizQuiz: common elements
95/450Interview / real lifeReal life: unique visitors
← Day 18Day 20 →