Loading

Quipoin Menu

Learn • Practice • Grow

Sets

A set is an unordered collection of unique, immutable elements. It is useful for removing duplicates, membership testing, and mathematical set operations (union, intersection, difference).

Creating Sets
Use curly braces or the `set()` constructor. Note: `{}` creates an empty dictionary, so for empty set use `set()`.


fruits = {"apple", "banana", "cherry"}
numbers = set([1, 2, 2, 3]) # {1,2,3}
empty_set = set()

Set Operations
  • `add(x)` – add element
  • `remove(x)` – remove element (raises error if not present)
  • `discard(x)` – remove element if present (no error)
  • `pop()` – remove and return arbitrary element
  • `clear()` – remove all

fruits.add("orange")
fruits.remove("banana")

Set Mathematics
  • `union()` or `|`
  • `intersection()` or `&`
  • `difference()` or `-`
  • `symmetric_difference()` or `^`
  • `issubset()`, `issuperset()`

A = {1, 2, 3}
B = {3, 4, 5}
print(A | B) # {1,2,3,4,5}
print(A & B) # {3}
print(A - B) # {1,2}
Two Minute Drill
  • Sets are unordered, unique, immutable elements.
  • Create with `{}` or `set()`, but `{}` is empty dict.
  • Add, remove, discard elements.
  • Support mathematical set operations (union, intersection, etc.).
  • Useful for deduplication and membership testing.

Need more clarification?

Drop us an email at career@quipoinfotech.com