Operations Sets with Python

Operations with Sets

Basic Set Operations

Adding Elements

You can add elements to a set using the add() method. If the element is already present, the set remains unchanged. 

# Creating a set
my_set = {1, 2, 3}
# Adding an element
my_set.add(4)
print(my_set)  # Output: {1, 2, 3, 4}
# Adding a duplicate element (no effect)
my_set.add(2)
print(my_set)  # Output: {1, 2, 3, 4}

Removing Elements

You can remove elements from a set using the remove() or discard() methods. The remove() method raises a KeyError if the element is not found, while discard() does not. 

# Removing an element
my_set.remove(3)
print(my_set)  # Output: {1, 2, 4}
# Removing an element that does not exist (raises KeyError)
# my_set.remove(10)  # Uncommenting this will raise KeyError
# Using discard() instead
my_set.discard(10)  # No error, even if element is not found
print(my_set)  # Output: {1, 2, 4}

Clearing a Set

You can remove all elements from a set using the clear() method. 

# Clearing all elements from the set
my_set.clear()
print(my_set)  # Output: set()

Set Operations

Union

The union of two sets is a set containing all elements from both sets. You can perform a union using the | operator or the union() method. 

set1 = {1, 2, 3}
set2 = {3, 4, 5}
# Using union() method
union_set = set1.union(set2)
print(union_set)  # Output: {1, 2, 3, 4, 5}
# Using | operator
union_set = set1 | set2
print(union_set)  # Output: {1, 2, 3, 4, 5}

Intersection

The intersection of two sets is a set containing only the elements that are present in both sets. Use the & operator or the intersection() method. 

# Intersection of sets
intersection_set = set1.intersection(set2)
print(intersection_set)  # Output: {3}
# Using & operator
intersection_set = set1 & set2
print(intersection_set)  # Output: {3}

Difference

The difference of two sets is a set containing elements that are in the first set but not in the second set. Use the – operator or the difference() method. 

# Difference of sets
difference_set = set1.difference(set2)
print(difference_set)  # Output: {1, 2}
# Using - operator
difference_set = set1 - set2
print(difference_set)  # Output: {1, 2}

Symmetric Difference

The symmetric difference of two sets is a set containing elements that are in either of the sets but not in both. Use the ^ operator or the symmetric_difference() method. 

# Symmetric difference of sets
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set)  # Output: {1, 2, 4, 5}
# Using ^ operator
symmetric_difference_set = set1 ^ set2
print(symmetric_difference_set)  # Output: {1, 2, 4, 5}

Set Comparisons

Subset

A set is a subset of another set if all elements of the first set are in the second set. Use the <= operator or the issubset() method. 

set_a = {1, 2}
set_b = {1, 2, 3}
# Using issubset() method
print(set_a.issubset(set_b))  # Output: True
# Using <= operator
print(set_a <= set_b)  # Output: True

Superset

A set is a superset of another set if it contains all elements of the second set. Use the >= operator or the issuperset() method. 

# Using issuperset() method
print(set_b.issuperset(set_a))  # Output: True
# Using >= operator
print(set_b >= set_a)  # Output: True

Disjoint Sets

Two sets are disjoint if they have no elements in common. Use the isdisjoint() method. 

set_x = {1, 2}
set_y = {3, 4}
# Check if sets are disjoint
print(set_x.isdisjoint(set_y))  # Output: True
set_z = {2, 3}
print(set_x.isdisjoint(set_z))  # Output: False

Set Comprehensions

Set comprehensions provide a concise way to create sets. They are similar to list comprehensions but produce sets. 

# Creating a set using set comprehension
squared_set = {x * x for x in range(5)}
print(squared_set)  # Output: {0, 1, 4, 9, 16}

 Set Methods

add(elem)

Adds an element to the set.

remove(elem)

Removes an element from the set. Raises a KeyError if the element is not present.

discard(elem)

Removes an element from the set if it is present. Does nothing if the element is not present.

pop()

Removes and returns an arbitrary element from the set. Raises KeyError if the set is empty.

copy()

Returns a shallow copy of the set. 

# Creating and copying a set
original_set = {1, 2, 3}
copied_set = original_set.copy()
print(copied_set)  # Output: {1, 2, 3}

Conclusion

Sets in Python provide a wide range of operations to perform mathematical set operations and manipulate collections of unique items. From basic operations like adding and removing elements to advanced operations like union, intersection, and symmetric difference, sets are highly versatile. Understanding these operations will help you manage and analyze collections of data efficiently.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print