Python Set Operators
Python provides operators to perform set operations like union, intersection, difference, and symmetric difference.
Python set ऑपरेटर की मदद से union, intersection, difference, symmetric difference जैसे operations कीए जाती हैं।
1. Union (| or union())
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 | set2)
print(set1.union(set2))
Output:
{1, 2, 3, 4, 5}
Combines both sets and removes duplicates.
दोनों sets को जोड़ता है और duplicate को हटा देता है।
2. Intersection (& or intersection())
print(set1 & set2)
print(set1.intersection(set2))
Output:
{3}
Returns only common elements.
सिर्फ common elements को return करता है।
3. Difference (- or difference())
print(set1 - set2)
print(set1.difference(set2))
Output:
{1, 2}
Items in set1 but not in set2.
Set1 में हैं लेकिन set2 में नहीं हैं।
4. Symmetric Difference (^ or symmetric_difference())
print(set1 ^ set2)
print(set1.symmetric_difference(set2))
Output:
{1, 2, 4, 5}
Items in either set but not in both.
ऐसे items जो एक ही set में हों, दोनों में नहीं।