Python Sets Explained for Beginners: Complete Guide with Examples

Python Sets Explained for Beginners (Complete Guide with Examples)

Python Sets Explained


Python programming me Set ek important data structure hai jo unique elements store karta hai.

Set ka main feature ye hai ki isme duplicate values allowed nahi hoti.

Example:

numbers = {1, 2, 3, 4, 5}
print(numbers)

Output

{1, 2, 3, 4, 5}

Agar duplicate values add karenge to Python automatically unhe remove kar deta hai.

Example:

numbers = {1, 2, 2, 3, 4}
print(numbers)

Output

{1, 2, 3, 4}

1. What is a Python Set?

Python Set ek unordered collection hai jisme:

✔ Duplicate values allowed nahi hoti
✔ Items random order me hote hain
✔ Curly brackets {} use hoti hain

Example:

fruits = {"apple", "banana", "mango"}
print(fruits)

2. Creating a Set

Python me set create karna bahut simple hai.

Example:

colors = {"red", "blue", "green"}
print(colors)

Empty set create karne ke liye:

my_set = set()

Note: {} empty dictionary banata hai, set nahi.


3. Adding Items in Set

Set me item add karne ke liye add() method use hota hai.

Example:

fruits = {"apple", "banana"}

fruits.add("mango")

print(fruits)

Output:

{'apple', 'banana', 'mango'}

4. Removing Items from Set

Set se item remove karne ke liye remove() method use hota hai.

Example:

fruits = {"apple", "banana", "mango"}

fruits.remove("banana")

print(fruits)

Output:

{'apple', 'mango'}

5. Loop Through a Set

Set ke items ko access karne ke liye loop use kar sakte hain.

Example:

fruits = {"apple", "banana", "mango"}

for fruit in fruits:
print(fruit)

Output:

apple
banana
mango

6. Python Set Operations

Python sets ka use mathematical operations ke liye bhi hota hai.

Union

Union ka matlab dono sets ka data combine karna.

Example:

A = {1, 2, 3}
B = {3, 4, 5}

print(A | B)

Output:

{1, 2, 3, 4, 5}

Intersection

Common elements find karne ke liye.

Example:

A = {1, 2, 3}
B = {2, 3, 4}

print(A & B)

Output:

{2, 3}

Difference

Ek set me jo element hai aur dusre me nahi.

Example:

A = {1, 2, 3}
B = {2, 3, 4}

print(A - B)

Output:

{1}

7. List vs Tuple vs Set Difference

FeatureListTupleSet
Symbol                            []                        ()    {}
Duplicate AllowedYes    Yes    No
OrderOrdered    Ordered    Unordered
MutableYes    No    Yes

Example:

List

numbers = [1,2,3]

Tuple

numbers = (1,2,3)

Set

numbers = {1,2,3}

8. Real Life Example of Sets

Set ka use duplicate values remove karne ke liye hota hai.

Example:

numbers = [1,2,2,3,4,4,5]

unique_numbers = set(numbers)

print(unique_numbers)

Output

{1,2,3,4,5}

Conclusion

Python Sets ek powerful data structure hai jo unique elements store karta hai aur mathematical operations perform karne me help karta hai.

Python programming me sets ka use:

• Duplicate data remove karne me
• Data comparison me
• Mathematical operations me

agar aap Python beginner hain, to sets ko samajhna bahut useful hai.

👉Read Previous Lesson:

1. Python Loops Explained: for Loop and while Loop with Examples for Beginners


Next Lesson:
9. Python Dictionaries Explained for Beginners (Complete Guide with Examples)


Comments

AI and the Future of Finance: How Artificial Intelligence is Transforming Money Management

2025's Best Free AI Financial Tools | Smart Money Apps

AI and the Future of Finance: How Artificial Intelligence is Transforming Money Management

🧮 Python Operators Explained with Examples (Beginner Friendly Guide)