🌱 Set Data Type
Last Updated: 17th August 2025
- A set is an unordered collection of unique items.
- Duplicates are automatically removed.
- Sets are mutable (can add/remove items), but items inside must be immutable (numbers, strings, tuples).
- Sets do not support indexing because they are unordered.
Hinglish Tip 🗣: Set ko socho jaise ek bag jisme unique cheezein rakhi jaati hain. Agar same item dobara add karoge, duplicate ignore ho jayega.
✏ Creating Sets
# empty set
s = set()
# using curly braces
fruits = {"apple", "banana", "cherry"}
# duplicates are removed automatically
nums = {1, 2, 2, 3} # {1,2,3}
# using set() constructor
letters = set("hello") # {'h','e','l','o'}
🔎 Accessing Items
fruits = {"apple", "banana", "cherry"}
for fruit in fruits:
print(fruit)
💡 Quick Practice
- Create a set of your favorite fruits.
- Loop through the set and print each fruit.
- Exercise