Python Tutorial in Hindi #10 - Sets

Python Sets Explained | No Duplicates, Set Methods & Operations | Mohit Decodes


In this beginner-friendly video, Mohit Decodes explains how to use Sets in Python to store unique values and perform efficient set operations. You'll learn all about creating sets, accessing items, adding/removing elements, and using useful set methods.


✅ Topics Covered:

Creating Sets

No Duplicates Allowed

Accessing Set Items

Adding & Removing Items

Useful Set Methods

Practical Examples


# --------------------------------------------
# Python Tutorial Day 10 - Sets
# By Mohit Decodes | Python Full Course in Hindi
# --------------------------------------------

# 1️⃣ Creating a Set
fruits = {"apple", "banana", "mango", "apple"} # Duplicate 'apple' ignored
print("Fruits Set:", fruits)
print("Type:", type(fruits))

# 2️⃣ Accessing Items (Looping)
for fruit in fruits:
print(fruit)

# 3️⃣ Adding Elements
fruits.add("kiwi")
print("After Add:", fruits)

# 4️⃣ Updating with Multiple Items
fruits.update(["orange", "grapes"])
print("After Update:", fruits)

# 5️⃣ Removing Elements
fruits.remove("banana") # ❗ Error if not found
print("After Remove:", fruits)

fruits.discard("mango") # ✅ No error if not found
print("After Discard:", fruits)

# 6️⃣ pop() - Removes Random Item
popped = fruits.pop()
print("Popped Element:", popped)
print("After Pop:", fruits)

# 7️⃣ clear() - Removes All Items
temp = fruits.copy()
temp.clear()
print("After Clear:", temp)

# 8️⃣ Set Length
print("Length of Fruits Set:", len(fruits))

# 9️⃣ Set Operations
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

print("Union:", A.union(B))
print("Intersection:", A.intersection(B))
print("Difference (A - B):", A.difference(B))
print("Symmetric Difference:", A.symmetric_difference(B))

# 🔟 Membership Testing
print(3 in A) # True
print(10 in A) # False

# ✅ Mini Challenge
# Create 2 sets and show common hobbies between two friends
friend1 = {"coding", "music", "cricket", "reading"}
friend2 = {"music", "traveling", "coding"}

common = friend1.intersection(friend2)
print("Common Hobbies:", common)