Welcome to Python Adventure! 🎮
Hello there, young coder! I'm Pythonix, your friendly Python tutor!
Today we're going on an exciting adventure to learn about something super cool called SETS! 🎉
🎁 What you can earn today:
What is a SET? 🤔
🎒 Imagine your school bag...
You have these items in your bag:
Wait! There are TWO books (📚)!
A SET is like a magic bag that keeps only ONE of each item! ✨
🔑 Key Points:
- A SET is a collection of unique items
- No duplicates allowed! 🚫
- Items have no specific order
🎯 Quick Question!
If you put 🍎🍌🍎🍊🍌 into a SET, how many items will remain?
Let's Learn Sets! 📚
✨ Creating a Set
To create a set in Python, we use curly braces { }
Just like how you list your favorite things! 🌟
fruits = {"apple", "banana", "orange"}
print(fruits)
{'apple', 'banana', 'orange'}
🤔 Think & Answer:
Which one creates a set correctly?
🚫 No Duplicates Allowed!
Sets are super smart! They automatically remove duplicates! 🧠
If you try to add the same thing twice, the set says "Nope, already got it!"
numbers = {1, 2, 2, 3, 3, 3}
print(numbers)
{1, 2, 3}
🤔 Think & Answer:
What will this print? print({5, 5, 5, 5})
➕ Adding Elements
Want to add something new to your set? Use .add()! 🎁
It's like putting a new toy in your toy box!
toys = {"ball", "car"}
toys.add("doll")
print(toys)
{'ball', 'car', 'doll'}
🤔 Think & Answer:
How do you add "mango" to a set called fruits?
➖ Removing Elements
To remove something, use .remove() or .discard()! 🗑️
Tip: .discard() is safer - it won't give an error if item doesn't exist!
colors = {"red", "blue", "green"}
colors.remove("blue")
print(colors)
{'red', 'green'}
🤔 Think & Answer:
Which removes "cat" from a set called pets?
🔍 Checking if Item Exists
Want to check if something is in your set? Use in! 🕵️
It answers with True or False - like a yes/no question!
snacks = {"chips", "cookies", "candy"}
print("chips" in snacks)
print("pizza" in snacks)
True
False
🤔 Think & Answer:
If games = {"chess", "ludo"}, what is "chess" in games?
🔗 Set Operations (Union & Intersection)
Union (|) = Combine all items from both sets! 🤝
Intersection (&) = Find items that are in BOTH sets! 🎯
set1 = {"a", "b", "c"}
set2 = {"b", "c", "d"}
print(set1 | set2) # Union
print(set1 & set2) # Intersection
{'a', 'b', 'c', 'd'}
{'b', 'c'}
🤔 Think & Answer:
What does {1,2} & {2,3} give us?
Quiz Time! ❓
Coding Zone! 👨💻
Mini Challenge: Magic School Bag! 🎒
🧙♂️ You have a magical school bag! Complete these tasks:
- Create a set called
bagwith items: "book", "pencil", "eraser" - Add "ruler" to the bag
- Remove "eraser" from the bag
- Print the final bag