An empty set is created with set(). {} creates an empty dictionary.
💡 Key Takeaways: Python Sets
Mastering Python Sets helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
Python Sets Code Example
Python
a = {"python", "sql", "html"}
b = {"html", "css", "javascript"}
print("Union:", a | b)
print("Both:", a & b)
print("Only in a:", a - b)
a.add("python") # duplicate: no change
a.add("git")
print(a)
print(len({1, 1, 2, 2, 3}))
Free sign-in required · Google login available
Python Sets Practice Exercises
Check what you learned in “Python Sets”.
/ 3 solved
Fill in the blank
Create an empty set.
tags =
✓ Correct!Not quite. Try again.
Multiple choice
What happens when you add a duplicate to a set?
✓ Correct!Not quite. Try again.
Code challenge
Print the items both sets have in common, sorted as a list.
Write the code in the live editor. Your answer is checked automatically.