Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In this tutorial, we'll demonstrate how to perform add, delete, intersection, union, and difference operations on Python sets.
Add
Use the add()
method to add an element to a set:
my_set = {1, 2, 3} my_set.add(4) print(my_set) # Output: {1, 2, 3, 4}
Delete
Use the remove()
or discard()
method to delete an element from a set:
# Using remove() my_set = {1, 2, 3} my_set.remove(1) print(my_set) # Output: {2, 3} # Using discard() my_set = {1, 2, 3} my_set.discard(1) print(my_set) # Output: {2, 3}
Intersection
Use the intersection()
method or the &
operator to find the intersection of two sets:
set_a = {1, 2, 3} set_b = {3, 4, 5} # Using intersection() method intersection_set = set_a.intersection(set_b) print(intersection_set) # Output: {3} # Using & operator intersection_set = set_a & set_b print(intersection_set) # Output: {3}
Union
Use the union()
method or the |
operator to find the union of two sets:
set_a = {1, 2, 3} set_b = {3, 4, 5} # Using union() method union_set = set_a.union(set_b) print(union_set) # Output: {1, 2, 3, 4, 5} # Using | operator union_set = set_a | set_b print(union_set) # Output: {1, 2, 3, 4, 5}
Difference
Use the difference()
method or the -
operator to find the difference of two sets:
set_a = {1, 2, 3} set_b = {3, 4, 5} # Using difference() method difference_set = set_a.difference(set_b) print(difference_set) # Output: {1, 2} # Using - operator difference_set = set_a - set_b print(difference_set) # Output: {1, 2}
How to Add Elements to a Python Set:
add()
method to add a single element, and update()
to add multiple elements.# Example my_set = {1, 2, 3} my_set.add(4) # Adds a single element my_set.update({5, 6}) # Adds multiple elements
Remove Element from Set in Python:
remove()
method to remove a specific element. If the element is not present, it raises a KeyError
. Use discard()
to avoid the error.# Example my_set = {1, 2, 3, 4} my_set.remove(2) # Removes element 2 my_set.discard(5) # Removes element 5 if present
Union of Sets in Python:
union()
method or the |
operator for the union of two sets.# Example set1 = {1, 2, 3} set2 = {3, 4, 5} union_set = set1.union(set2) # or use set1 | set2
Set Difference Operation in Python:
difference()
method or the -
operator for the set difference.# Example set1 = {1, 2, 3} set2 = {3, 4, 5} difference_set = set1.difference(set2) # or use set1 - set2
Deleting Elements from a Set in Python:
remove()
or discard()
method to delete specific elements.# Example my_set = {1, 2, 3, 4} my_set.remove(2) # Removes element 2 my_set.discard(5) # Removes element 5 if present
Adding Multiple Elements to a Set in Python:
update()
method or the |
operator to add multiple elements.# Example my_set = {1, 2, 3} my_set.update({4, 5, 6}) # or use my_set |= {4, 5, 6}
Set Intersection Using '&' Operator in Python:
intersection()
method or the &
operator for the set intersection.# Example set1 = {1, 2, 3} set2 = {3, 4, 5} intersection_set = set1.intersection(set2) # or use set1 & set2
Set Union Using '|' Operator in Python:
union()
method or the |
operator for the set union.# Example set1 = {1, 2, 3} set2 = {3, 4, 5} union_set = set1.union(set2) # or use set1 | set2
Symmetric Difference of Sets in Python:
symmetric_difference()
method or the ^
operator for the symmetric difference.# Example set1 = {1, 2, 3} set2 = {3, 4, 5} symmetric_difference_set = set1.symmetric_difference(set2) # or use set1 ^ set2