A Set in Python is an unordered collection of unique elements. Sets are useful when you want to store non-duplicate items and perform mathematical operations like union, intersection, etc.
Python में Set एक unordered और unique elements का collection होता है। Set का उपयोग ऐसे data को store करने में किया जाता है जिसमें डुप्लीकेट न हों और mathematical operations जैसे union, intersection करना हो।
my_set = {1, 2, 3, 4}
print(my_set)
Output:
{1, 2, 3, 4}
my_set.add(5)
print(my_set)
Output:
{1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set)
Output:
{1, 2, 4, 5}
for item in my_set:
print(item)
Output:
1
2
4
5
a = {1, 2, 3}
b = {3, 4, 5}
print(a.union(b))
Output:
{1, 2, 3, 4, 5}
print(a.intersection(b))
Output:
{3}
print(len(my_set))
Output:
4