Introduction
In this lab, you will learn how to check if a set is empty in Python. This involves understanding the concept of empty sets and utilizing different methods to determine their emptiness.
The lab guides you through creating an empty set using the set() constructor and verifying its type. You'll then explore how to use the len() function to check if a set is empty by determining the number of elements it contains. Finally, you'll learn how to evaluate a set as a boolean value to check for emptiness.
Learn About Empty Sets
In this step, you will learn about empty sets in Python. An empty set is a set that contains no elements. It's a fundamental concept in set theory and is often used in programming to represent a collection that is initially empty but may later contain elements.
To create an empty set in Python, you can use the set() constructor without any arguments. Let's create an empty set and print it to see what it looks like.
Open the VS Code editor in the LabEx environment.
Create a new file named
empty_set.pyin the~/projectdirectory.~/project/empty_set.pyAdd the following Python code to the
empty_set.pyfile:## Create an empty set empty_set = set() ## Print the empty set print(empty_set) ## Check the type of the variable print(type(empty_set))This code first creates an empty set using
set()and assigns it to the variableempty_set. Then, it prints the value ofempty_setand its type using thetype()function.Run the Python script using the following command in the terminal:
python ~/project/empty_set.pyYou should see the following output:
set() <class 'set'>The output
set()indicates that you have successfully created an empty set. The<class 'set'>confirms that the variableempty_setis indeed a set object.An empty set is different from
Noneor an empty list[]. It's a specific type of collection that represents the absence of elements. Understanding how to create and use empty sets is crucial for various programming tasks, such as initializing a collection before adding elements or checking if a set is empty before performing certain operations.
Use len() to Check
In this step, you will learn how to use the len() function to check if a set is empty. The len() function returns the number of elements in a set. For an empty set, it will return 0. This is a useful way to programmatically determine whether a set contains any elements.
Building upon the previous step, let's modify the empty_set.py file to include the len() function.
Open the
empty_set.pyfile in the VS Code editor. It should be located in the~/projectdirectory.Modify the
empty_set.pyfile to include the following Python code:## Create an empty set empty_set = set() ## Print the empty set print(empty_set) ## Check the type of the variable print(type(empty_set)) ## Check the length of the empty set print(len(empty_set))In this code, we've added a line that uses the
len()function to determine the number of elements inempty_setand prints the result.Run the Python script using the following command in the terminal:
python ~/project/empty_set.pyYou should see the following output:
set() <class 'set'> 0The output
0confirms that thelen()function correctly reports that the empty set has zero elements.You can also use
len()to check the size of non-empty sets. For example, if you add elements to the set,len()will return the number of elements added. This makeslen()a versatile tool for working with sets in Python.
Evaluate as Boolean
In this step, you will learn how an empty set evaluates as a boolean value in Python. In Python, certain values are considered "truthy" (evaluate to True in a boolean context) and others are considered "falsy" (evaluate to False). An empty set is considered a "falsy" value. This means that when you use an empty set in a conditional statement (like an if statement), it will be treated as False.
Let's modify the empty_set.py file to demonstrate this behavior.
Open the
empty_set.pyfile in the VS Code editor. It should be located in the~/projectdirectory.Modify the
empty_set.pyfile to include the following Python code:## Create an empty set empty_set = set() ## Print the empty set print(empty_set) ## Check the type of the variable print(type(empty_set)) ## Check the length of the empty set print(len(empty_set)) ## Evaluate the empty set as a boolean if empty_set: print("The set is not empty") else: print("The set is empty")In this code, we've added an
ifstatement that checks the boolean value ofempty_set. Ifempty_setis "truthy" (not empty), it will print "The set is not empty". Otherwise, it will print "The set is empty".Run the Python script using the following command in the terminal:
python ~/project/empty_set.pyYou should see the following output:
set() <class 'set'> 0 The set is emptyThe output "The set is empty" confirms that the empty set is evaluated as
Falsein a boolean context.This behavior is useful for writing concise and readable code. For example, you can check if a set has any elements before performing an operation that requires elements to be present. Understanding how empty sets evaluate as booleans is an important aspect of working with sets in Python.
Summary
In this lab, you learned about empty sets in Python and how to create them using the set() constructor without arguments. You also learned how to verify that a variable is indeed a set object using the type() function.
Furthermore, you started learning how to use the len() function to determine if a set is empty by checking the number of elements it contains.



