Python Sets Tutorial | What is Set, Why & When to Use, Advantages & 10 Examples

Python Sets Tutorial

A set in Python is an unordered collection of unique items. Sets are mutable but do not allow duplicate elements. They are commonly used to perform operations like union, intersection, and difference.

Python में set एक unordered collection होती है जिसमें unique items होते हैं। Sets mutable होती हैं लेकिन duplicate elements allow नहीं करती। इनका उपयोग union, intersection, difference जैसे operations के लिए होता है।

Why Use Sets?

  • To store unique elements only.
  • To perform mathematical set operations efficiently.
  • When order of elements does not matter.
  • केवल unique elements स्टोर करने के लिए।
  • mathematical set operations को efficient तरीके से करने के लिए।
  • जब elements का क्रम महत्वपूर्ण न हो।

When to Use Sets?

  • Removing duplicates from a list.
  • Checking membership (fast lookup).
  • Performing unions, intersections, differences of data.
  • List से duplicates हटाने के लिए।
  • membership check (तेजी से lookup) के लिए।
  • union, intersection, difference operations के लिए।

Advantages of Sets

  • Stores only unique elements.
  • Faster membership testing than lists or tuples.
  • Supports mathematical set operations directly.
  • Mutable and dynamic size.
  • केवल unique elements स्टोर करती है।
  • lists या tuples की तुलना में membership test तेज होता है।
  • mathematical set operations को सीधे सपोर्ट करती है।
  • Mutable और dynamic size होती है।
Example 1: Create a Set
myset = {1, 2, 3, 4}
print(myset)

Create a set with 4 unique numbers.

4 unique नंबर वाला set बनाएं।

Output:

{1, 2, 3, 4}
Example 2: Set with Duplicate Items
myset = {1, 2, 2, 3, 3, 3}
print(myset)

Duplicates are automatically removed in sets.

Duplicates अपने आप हट जाते हैं।

Output:

{1, 2, 3}
Example 3: Add Item to Set
myset = {1, 2, 3}
myset.add(4)
print(myset)

Add a new item using add() method.

add() से नया item जोड़ें।

Output:

{1, 2, 3, 4}
Example 4: Remove Item from Set
myset = {1, 2, 3, 4}
myset.remove(3)
print(myset)

Remove an item using remove().

remove() से item हटाएं।

Output:

{1, 2, 4}
Example 5: Check Membership
myset = {1, 2, 3}
print(2 in myset)
print(5 in myset)

Check if items are present in set.

देखें कि items set में हैं या नहीं।

Output:

True
False
Example 6: Union of Sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2))

Get all unique items from both sets.

दोनों sets के unique items लें।

Output:

{1, 2, 3, 4, 5}
Example 7: Intersection of Sets
set1 = {1, 2, 3}
set2 = {2, 3, 4}
print(set1.intersection(set2))

Get common items in both sets.

दोनों sets के common items लें।

Output:

{2, 3}
Example 8: Difference of Sets
set1 = {1, 2, 3}
set2 = {2, 3, 4}
print(set1.difference(set2))

Get items in set1 but not in set2.

set1 में लेकिन set2 में नहीं वाले items।

Output:

{1}
Example 9: Clear a Set
myset = {1, 2, 3}
myset.clear()
print(myset)

Remove all items from the set.

Set के सभी items हटाएं।

Output:

set()
Example 10: Iterate Through a Set
myset = {1, 2, 3}
for item in myset:
    print(item)

Loop through all items in a set.

Set के सभी items पर loop चलाएं।

Output:

1
2
3