Sets
A set is an unordered collection of unique elements. Sets are similar to lists, but they have some important differences.
- Unordered: Sets are unordered, which means that the elements do not have a specific order.
- An unordered set in Python, also known as a set, is a built-in data structure that stores a collection of unique elements. Unlike other data structures like lists or tuples, sets do not maintain any specific order of the elements. This means that the elements of a set are not indexed, and you cannot access them by their position.
The main characteristic of a set is that it does not allow duplicate elements. If you try to add an element to a set that is already present, it will not be added again. Sets are useful when you need to store a collection of distinct items and perform operations like union, intersection, and difference between sets.
- Unique: Sets are unique, which means that no element can appear more than once in a set.
- A unique set, also known as a set, is a data structure in Python that
stores a collection of distinct elements. It ensures that there are no
duplicate values within the set. The uniqueness property of sets is
achieved by using hash-based storage and equality comparisons. When you
add an element to a set that is already present, it won't be added
again. Sets are useful when you want to store a collection of unique
items and perform operations like membership testing, set operations, or
eliminating duplicates from other data structures. In Python, sets are
denoted by curly braces
{}
or by using theset()
function.
- Mutable: Sets are mutable, which means that the elements in a set can be changed.
In Python, sets are inherently mutable data structures. This means that you can modify the contents of a set after it has been created. Unlike immutable data types such as tuples or strings, which cannot be modified once created, mutable sets allow you to add, remove, or update elements.
To modify a set, you can use various methods and operations available for sets. For example, you can use the
add()
method to add an element to a set, theremove()
method to remove an element from a set, or theupdate()
method to add multiple elements from another iterable to a set.my_set = {1, 2, 3, 4, 5} print(my_set) # Output: {1, 2, 3, 4, 5} my_set.add(6) print(my_set) # Output: {1, 2, 3, 4, 5, 6} my_set.remove(2) print(my_set) # Output: {1, 3, 4, 5, 6} new_elements = {7, 8, 9} my_set.update(new_elements) print(my_set) # Output: {1, 3, 4, 5, 6, 7, 8, 9}
Creating Sets
Sets can be created using the set()
constructor. The set()
constructor takes a sequence as its argument and creates a set from the elements of the sequence. For example, the following code creates a set from the list [1, 2, 3, 4, 5]
:
set([1, 2, 3, 4, 5])
Adding Elements to Sets
Elements can be added to sets using the add()
method. The add()
method takes an element as its argument and adds the element to the set. For example, the following code adds the element 6
to the set s
:
s = set([1, 2, 3, 4, 5])
s.add(6)
Removing Elements from Sets
Elements can be removed from sets using the remove()
method. The remove()
method takes an element as its argument and removes the element from the set. If the element does not exist in the set, a KeyError
exception is raised. For example, the following code removes the element 6
from the set s
:
s = set([1, 2, 3, 4, 5])
s.remove(6)
Checking for Set Membership
The in
operator can be used to check if an element is a member of a set. The in
operator returns True
if the element is a member of the set, and False
otherwise. For example, the following code checks if the element 6
is a member of the set s
:
s = set([1, 2, 3, 4, 5])
if 6 in s:
print("6 is a member of s")
else:
print("6 is not a member of s")
Set Operations
Sets support a variety of operations, including union, intersection, difference, and symmetric difference.
- Union: The union of two sets is a set that contains all of the elements in both sets.
- Intersection: The intersection of two sets is a set that contains all of the elements that are in both sets.
- Difference: The difference of two sets is a set that contains all of the elements in the first set that are not in the second set.
- Symmetric difference: The symmetric difference of two sets is a set that contains all of the elements that are in either the first set or the second set, but not both.
For example, the following code computes the union, intersection, difference, and symmetric difference of the sets s
and t
:
s = set([1, 2, 3, 4, 5])
t = set([3, 4, 5, 6, 7])
union = s | t
intersection = s & t
difference = s - t
symmetric_difference = s ^ t
print(union)
print(intersection)
print(difference)
print(symmetric_difference)

Comments
Post a Comment