Skip to main content

What are sets?

Sets are collections that only store unique values. They automatically remove duplicates. Think of sets like:
  • A bag of unique marbles
  • Guest list (each person once)
  • Unique tags or categories

Creating sets

You can create sets two ways: with set() or with curly braces {} (but only when it has values).
Use set() for empty sets, not {}. Empty curly braces create a dictionary!

Basic operations

Mathematical Set Operations

One of the most powerful features of sets is performing mathematical set algebra. Python provides both methods and operators for these:

1. Union (|)

Combines all unique elements from both sets:

2. Intersection (&)

Finds elements that are present in both sets:

3. Difference (-)

Finds elements present in the first set but not in the second set:

4. Symmetric Difference (^)

Finds elements present in either of the sets, but not in both:

5. Subsets & Supersets

Checks the relationships between sets:

Common uses

Remove duplicates

Fast membership testing

Common mistakes

Practice & Exercises

To reinforce what you’ve learned in this section (Sets, unique collections, and set algebra operations), practice with these interactive notebooks:

Follow-Along Practice

Practice initializing sets, removing duplicates from lists, and applying union/intersection checks.💻 VS Code | 🚀 Colab | 📥 Download

Practice Exercises

Test your knowledge with hands-on exercises on duplicate removal and shared skill intersections.💻 VS Code | 🚀 Colab | 📥 Download

What’s next?

Learn about how to handle Queues (First-In, First-Out) efficiently in Python using double-ended queues.

Queues

Learn to manage queues and FIFO structures