🧙♂️ Python Sets: A Wizard’s Guide to Unordered Power
🧙♂️ The Tale of the Mystic Sets
Once upon a time in the magical land of Pythonia, there lived three wizards: Roger, Syd, and Luna. These wizards were always up to cool tricks, and today, they discovered a powerful magical object called a Set.
Now, Sets in Pythonia were special. They weren’t like scrolls (aka lists 📜) where things were arranged in order. Nope. Sets didn’t care about who came first or last. And unlike potions (tuples), they weren’t fixed forever—they could change! 💫
So, here’s how the wizards created one:
names = {"Roger", "Syd"}
Poof! A set named names
appeared, filled with “Roger” and “Syd”. Just like that. No spells needed.
⚔️ The Battle of the Intersecting Sets
One day, Roger was curious. He wondered, “Which of my friends are also friends with Luna?”
So they created two sets:
set1 = {"Roger", "Syd"}
set2 = {"Roger"}
To find out who was in both sets, they used the intersection spell:
intersect = set1 & set2
print(intersect) # {'Roger'}
Only Roger appeared in both places. So the result was {'Roger'}
. Nice!
🫱🏽🫲🏾 The Union of Realms
Now Luna wanted to combine forces. She used the union spell to bring everyone together:
set1 = {"Roger", "Syd"}
set2 = {"Luna"}
union = set1 | set2
print(union) # {'Roger', 'Syd', 'Luna'}
Just like that, all three wizards were united in one mighty set.
🧹 The Clean-Up – Finding Differences
But Syd noticed something strange. “Wait,” Syd said, “who’s in my team but not in Roger’s?” So they used the difference spell:
set1 = {"Roger", "Syd"}
set2 = {"Roger"}
difference = set1 - set2
print(difference) # {'Syd'}
Turns out it was just Syd! Mystery solved 🔍.
👑 The Royal Check – Superset and Subset
Roger got a little proud. “Am I the big boss here? Is my set a superset of yours?” he asked Syd.
set1 = {"Roger", "Syd"}
set2 = {"Roger"}
isSuperset = set1 > set2
print(isSuperset) # True
Yup! Roger’s set had everything Syd had… and more.
📦 Counting and Converting the Set
Later, Luna wanted to count how many friends were in the group:
names = {"Roger", "Syd"}
print(len(names)) # 2
Easy peasy—just 2!
She also wanted to turn the set into a list (because scrolls are prettier):
list_names = list(names)
print(list_names) # ['Roger', 'Syd']
Boom! Just like that, the set became a scroll (a list)! 🎉
🧐 Membership Check
One last thing. Roger asked, “Am I even in this set?”
print("Roger" in names) # True
And the answer came back: True. You’re in, my wizardly friend.
✨ Moral of the Story:
Sets in Python are like magical bags:
- They hold unique items.
- They don’t care about order.
- You can combine them, compare them, subtract from them, and even change them—unless it’s a frozenset (more on that icy fellow later ❄️).
- They’re fast, super useful when you just want to know who’s in or who’s out, no duplicates allowed.
✅ Review Time! Let’s check what you’ve learned:
- What happens when you add duplicate values to a Python set?
- How do you find common elements between two sets?
- What will
len({"Roger", "Roger", "Syd"})
return? - How do you check if a set is a subset of another?
- Turn this set into a list:
{"Code", "Debug", "Repeat"}
. What function do you use?
Keep playing with sets like you’re in your own Python adventure game. Who knows? Maybe one day you’ll create the next wizard-level program that saves the world. 🌍💻