Python Sets

Set Basics

Sets are unordered collections of unique elements defined using curly braces.

Set Methods

  • add(), remove().
  • union(), intersection().

Examples

nums = {1, 2, 3}
nums.add(4)
print(nums)  # {1, 2, 3, 4}

setA = {1, 2}
setB = {2, 3}
print(setA.union(setB))          # {1, 2, 3}