Sets in Python:
Sets in Python are unordered collections of unique elements. A set is similar to a list or tuple, but it has the following differences:
Sets are unordered, which means that the elements are not stored in any particular order.
Sets only contain unique elements, which means that duplicates are not allowed.
Sets are mutable, which means that you can change the elements of a set after it has been created.
Creating Sets in Python
You can create a set in Python by using the set() function or by enclosing a list of elements in curly braces {}.
For example, you can create a set of numbers as follows:
python>>> numbers = set([1, 2, 3, 4, 5])
>>> print(numbers)
{1, 2, 3, 4, 5}
You can also create a set of strings as follows:
python>>> words = {'apple', 'banana', 'cherry'}
>>> print(words)
{'banana', 'apple', 'cherry'}
Accessing Elements in a Set
You can access the elements in a set by using a for loop or by using the in operator.
For example, you can print the elements in a set of numbers as follows:
python>>> for num in numbers:
... print(num)
...
1
2
3
4
5
You can also check if an element is in a set by using the in operator. For example:
python>>> print(3 in numbers)
True
>>> print(6 in numbers)
False
Adding Elements to a Set
You can add elements to a set by using the add() method or by using the update() method.
For example, you can add the number 6 to a set of numbers as follows:
python>>> numbers.add(6)
>>> print(numbers)
{1, 2, 3, 4, 5, 6}
You can also add multiple elements to a set by using the update() method. For example:
python>>> numbers.update([7, 8, 9])
>>> print(numbers)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
Removing Elements from a Set
You can remove elements from a set by using the remove() method or the discard() method.
For example, you can remove the number 4 from a set of numbers as follows:
python>>> numbers.remove(4)
>>> print(numbers)
{1, 2, 3, 5, 6, 7, 8, 9}
You can also remove an element from a set without raising an error if the element is not found by using the discard() method. For example:
python>>> numbers.discard(4)
>>> print(numbers)
{1, 2, 3, 5, 6, 7, 8, 9}
Set Operations
There are several set operations that you can perform in Python, including union, intersection, difference, and symmetric difference.
For example, you can find the union of two sets by using the union() method or the | operator. For example:
set1 = {1, 2, 3, 4
, 5}
set2 = {4, 5, 6, 7, 8}
set3 = set1.union(set2)
print(set3)
{1, 2, 3, 4, 5, 6, 7, 8}
Or using the | operator:
python>>> set3 = set1 | set2
>>> print(set3)
{1, 2, 3, 4, 5, 6, 7, 8}
You can find the intersection of two sets by using the intersection() method or the & operator. For example:
python>>> set3 = set1.intersection(set2)
>>> print(set3)
{4, 5}
Or using the & operator:
set3 = set1 & set2
print(set3)
{4, 5}
You can find the difference of two sets by using the difference() method or the - operator. For example:
set3 = set1.difference(set2)
print(set3)
{1, 2, 3}
Or using the - operator:
python>>> set3 = set1 - set2
>>> print(set3)
{1, 2, 3}
You can find the symmetric difference of two sets by using the symmetric_difference() method or the ^ operator. For example:
python>>> set3 = set1.symmetric_difference(set2)
>>> print(set3)
{1, 2, 3, 6, 7, 8}
Or using the ^ operator:
set3 = set1 ^ set2
print(set3)
{1, 2, 3, 6, 7, 8}
Conclusion
Sets in Python are useful data structures for storing unique elements. With sets, you can perform various set operations, such as union, intersection, difference, and symmetric difference. Knowing how to work with sets in Python will make you a more efficient and effective programmer.
By itsbilyat
Comments
Post a Comment