🐍 Python Sets for Beginners
📚 Table of Contents
- What is a Set?
- Creating a Set
- Set Properties
- Adding Elements
- Removing Elements
- Set Operations
- Common Set Methods
- Practice Examples
- FAQ
💡 Quick Tip
Sets are useful when you want to store unique values and remove duplicates.
⚠️ Common Mistakes
- Using duplicate values expecting multiple outputs.
- Using index numbers because sets are unordered.
- Using empty {} for creating an empty set.
✍️ By Roshni Code Charm
📅 July 2026 | ⏱️ 5 min read
🐍 What is a Set?
A set is a collection of unique values.
Sets are unordered and do not allow duplicate elements.
Creating a Set
numbers = {10,20,30,40}
print(numbers)
Set with Duplicate Values
Duplicate values are automatically removed from a set.
numbers = {10,20,20,30}
print(numbers)
✨ Properties of Sets
- Sets contain unique values.
- Sets are unordered.
- Sets are changeable.
- Sets do not support indexing.
➕ Adding Elements
fruits = {"Apple","Mango"}
fruits.add("Banana")
print(fruits)
➖ Removing Elements
colors = {"Red","Blue","Green"}
colors.remove("Blue")
print(colors)
🔄 Set Operations
- Union ( | ) - Combines two sets
- Intersection ( & ) - Common values
- Difference ( - ) - Different values
✨ Common Set Methods
- add() - Add new element
- remove() - Remove element
- clear() - Remove all elements
- len() - Count elements
🚀 Practice Example
languages = {"Python","HTML","CSS"}
languages.add("JavaScript")
print(languages)
❓ Frequently Asked Questions
1. Can sets contain duplicate values?
No, sets automatically remove duplicate values.
2. Do sets have indexes?
No, sets are unordered and do not support indexing.
3. Which brackets are used for sets?
Curly brackets {} are used to create sets.