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 cover the basics of Python sets and their operations. Sets are unordered collections of unique elements. They are useful for membership testing, removing duplicates from a sequence, and performing mathematical set operations like union, intersection, and difference.
1. Creating a Set
You can create a set by using the set()
constructor or by using curly braces {}
.
# Using the set constructor my_set = set([1, 2, 3, 4, 5]) # Using curly braces my_set = {1, 2, 3, 4, 5}
Note: An empty set can only be created using the set()
constructor, as using {}
will create an empty dictionary instead.
empty_set = set()
2. Adding Elements to a Set
You can add elements to a set using the add()
method.
my_set = {1, 2, 3} my_set.add(4) print(my_set) # Output: {1, 2, 3, 4}
3. Removing Elements from a Set
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.
my_set = {1, 2, 3, 4} my_set.remove(3) # Removes 3 from the set my_set.discard(5) # Does nothing as 5 is not in the set print(my_set) # Output: {1, 2, 4}
4. Set Operations
Python sets support various operations, such as union, intersection, difference, and symmetric difference.
set_a = {1, 2, 3} set_b = {3, 4, 5} union_set = set_a | set_b print(union_set) # Output: {1, 2, 3, 4, 5}
intersection_set = set_a & set_b print(intersection_set) # Output: {3}
difference_set = set_a - set_b print(difference_set) # Output: {1, 2}
symmetric_difference_set = set_a ^ set_b print(symmetric_difference_set) # Output: {1, 2, 4, 5}
5. Set Membership
You can check if an element is a member of a set using the in
keyword.
my_set = {1, 2, 3, 4, 5} print(1 in my_set) # Output: True print(6 in my_set) # Output: False
6. Set Length
You can find the number of elements in a set using the len()
function.
my_set = {1, 2, 3, 4, 5} print(len(my_set)) # Output: 5
How to Create a Set in Python:
{}
or the set()
constructor to create a set.# Example my_set = {1, 2, 3}
Set Data Type in Python:
# Example my_set = {1, 2, 3}
Python Set Methods and Operations:
add()
, remove()
, union()
, intersection()
, etc.# Example my_set.add(4) # Add an element my_set.remove(2) # Remove an element union_set = set1.union(set2) # Union of two sets intersection_set = set1.intersection(set2) # Intersection of two sets
Accessing and Modifying Set Elements in Python:
add()
and remove()
to modify.# Example my_set.add(4) # Add an element my_set.remove(2) # Remove an element
Iterating Through a Set in Python:
# Example for element in my_set: print(element)
Set Operations in Python (Union, Intersection, etc.):
# Example set1 = {1, 2, 3} set2 = {2, 3, 4} union_set = set1.union(set2) intersection_set = set1.intersection(set2)
Set Comprehension in Python:
# Example squares = {x**2 for x in range(5)}
Converting List to Set in Python:
set()
constructor or {}
to convert a list to a set.# Example my_list = [1, 2, 3, 2, 1] my_set = set(my_list) # Removes duplicates
Common Operations on Python Sets:
add()
, remove()
, union()
, intersection()
, difference()
, and more.# Example my_set.add(4) # Add an element my_set.remove(2) # Remove an element union_set = set1.union(set2) # Union of two sets intersection_set = set1.intersection(set2) # Intersection of two sets