Set Methods Part 1| Python 4 You | Lecture 142
Exploring Set Methods in Python: A Comprehensive Guide
Sets are a versatile and fundamental data structure in Python used for storing collections of unique elements. They provide several built-in methods that allow you to manipulate and work with sets efficiently. In this guide, we will explore various set methods in Python and understand how to use them effectively.
Common Set Methods:
Now, let's explore some of the most commonly used set methods in Python:
add(element) - Adding Elements to a Set:
The add method is used to add a single element to a set. If the element is already present, it won't be added again.
python code
my_set = {1, 2, 3}
my_set.add(4)
After this operation, my_set will be {1, 2, 3, 4}.
update(iterable) - Adding Multiple Elements:
The update method is used to add multiple elements to a set from an iterable (e.g., a list or another set). It eliminates duplicates during the update.
python code
my_set = {1, 2, 3}
my_set.update([3, 4, 5])
After this operation, my_set will be {1, 2, 3, 4, 5}.
remove(element) - Removing Elements:
The remove method deletes a specified element from the set. If the element doesn't exist in the set, it raises a KeyError.
python code
my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
After this operation, my_set will be {1, 2, 4, 5}.
discard(element) - Safely Removing Elements:
The discard method is similar to remove, but it won't raise an error if the specified element doesn't exist in the set.
python code
my_set = {1, 2, 3, 4, 5}
my_set.discard(6)
my_set will still be {1, 2, 3, 4, 5} after this operation.
pop() - Removing and Returning an Element:
The pop method removes and returns an arbitrary element from the set. Since sets are unordered, which element is popped is not predictable.
python code
my_set = {1, 2, 3, 4, 5}
popped_element = my_set.pop()
popped_element could be any of the elements in the set.
clear() - Emptying a Set:
The clear method empties the set, making it an empty set.
python code
my_set = {1, 2, 3, 4, 5}
my_set.clear()
After this operation, my_set will be an empty set, {}.
copy() - Creating a Shallow Copy:
The copy method returns a shallow copy of the set. Changes made to the copied set do not affect the original.
python code
my_set = {1, 2, 3}
new_set = my_set.copy()
new_set is a copy of my_set.
union(other_set) - Set Union:
The union method returns a new set containing all the unique elements from both sets.
python code
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
union_set contains {1, 2, 3, 4, 5}.
intersection(other_set) - Set Intersection:
The intersection method returns a new set containing elements that are common to both sets.
python code
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
intersection_set = set1.intersection(set2)
intersection_set contains {3, 4, 5}.
difference(other_set) - Set Difference:
The difference method returns a new set with elements from the first set that are not in the second set.
python code
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
difference_set contains {1, 2}.
symmetric_difference(other_set) - Symmetric Set Difference:
The symmetric_difference method returns a new set with elements that are unique to each set, excluding common elements.
python code
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
symmetric_difference_set = set1.symmetric_difference(set2)
symmetric_difference_set contains {1, 2, 6, 7}.
issubset(other_set) and issuperset(other_set) - Subset and Superset Checks:
The issubset and issuperset methods are used to check if a set is a subset or superset of another set.
python code
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
is_subset = set1.issubset(set2)
is_superset = set2.issuperset(set1)
is_subset will be True, and is_superset will also be True.
#python4 #pythontutorial #pythonprogramming #python3 #pythonforbeginners #pythonlectures #pythonprograms #pythonlatest #rehanblogger #python4you #pythonlatestversion #pythonlatestversion Learn python3.12.0 and latest version of python3.13. If you are searching for python3.13.0 lessons, you are at the right place as this course will be very helpful for python learners or python beginners.
Видео Set Methods Part 1| Python 4 You | Lecture 142 автора Программистское Мастерство
Видео Set Methods Part 1| Python 4 You | Lecture 142 автора Программистское Мастерство
Информация
2 декабря 2023 г. 14:17:19
00:17:27
Похожие видео