Introduction
In this lab, you will learn how to check if a set contains unique elements in Python. Sets are a built-in data type that store unordered collections of unique elements, automatically eliminating duplicates. You'll start by creating and printing sets, observing how duplicate values are handled.
The lab will then delve deeper into the properties of sets, exploring how to leverage their unique characteristics for tasks such as removing duplicate entries or checking for element presence. You will learn to verify uniqueness using len() and element count methods.
Learn About Set Uniqueness
In this step, you will learn about the fundamental concept of set uniqueness in Python. Sets are a built-in data type in Python that store an unordered collection of unique elements. This means that a set cannot contain duplicate values. Understanding this property is crucial for efficiently managing and manipulating data where uniqueness is important.
Let's start by creating a simple set:
Open the VS Code editor in the LabEx environment.
Create a new file named
set_example.pyin the~/projectdirectory.## Create a set of numbers my_set = {1, 2, 3, 4, 5} ## Print the set print(my_set)Save the file.
Run the script using the
pythoncommand in the terminal:python set_example.pyYou should see the following output:
{1, 2, 3, 4, 5}
Now, let's see what happens when we try to add duplicate elements to a set:
Modify the
set_example.pyfile to include duplicate values:## Create a set with duplicate numbers my_set = {1, 2, 2, 3, 4, 4, 5} ## Print the set print(my_set)Save the file.
Run the script again:
python set_example.pyYou should see the following output:
{1, 2, 3, 4, 5}
Notice that even though we tried to add duplicate values (2 and 4), the set only contains unique elements. This demonstrates the core property of sets: they automatically eliminate duplicates.
This uniqueness property makes sets very useful for tasks such as removing duplicate entries from a list or checking if an element is already present in a collection. In the following steps, we will explore more applications of sets and how to leverage their unique characteristics.
Understand Set Properties
In this step, we will delve deeper into the properties of sets in Python. Sets are more than just collections of unique elements; they also support various operations that make them a powerful tool for data manipulation. We will explore how to add elements, remove elements, and perform common set operations like union, intersection, and difference.
Let's start by adding elements to a set:
Open the
set_example.pyfile in the~/projectdirectory using the VS Code editor.Modify the file to add elements to the set using the
add()method:## Create a set my_set = {1, 2, 3} ## Add elements to the set my_set.add(4) my_set.add(5) ## Print the set print(my_set)Save the file.
Run the script using the
pythoncommand in the terminal:python set_example.pyYou should see the following output:
{1, 2, 3, 4, 5}
Now, let's see how to remove elements from a set:
Modify the
set_example.pyfile to remove an element using theremove()method:## Create a set my_set = {1, 2, 3, 4, 5} ## Remove an element from the set my_set.remove(3) ## Print the set print(my_set)Save the file.
Run the script again:
python set_example.pyYou should see the following output:
{1, 2, 4, 5}Note that if you try to remove an element that is not in the set, a
KeyErrorwill be raised. To avoid this, you can use thediscard()method, which does not raise an error if the element is not present.## Create a set my_set = {1, 2, 3, 4, 5} ## Discard an element from the set my_set.discard(6) ## No error raised ## Print the set print(my_set)
Finally, let's explore some common set operations:
Modify the
set_example.pyfile to perform union, intersection, and difference operations:## Create two sets set1 = {1, 2, 3, 4, 5} set2 = {3, 4, 5, 6, 7} ## Union of the sets union_set = set1.union(set2) print("Union:", union_set) ## Intersection of the sets intersection_set = set1.intersection(set2) print("Intersection:", intersection_set) ## Difference of the sets (elements in set1 but not in set2) difference_set = set1.difference(set2) print("Difference:", difference_set)Save the file.
Run the script again:
python set_example.pyYou should see the following output:
Union: {1, 2, 3, 4, 5, 6, 7} Intersection: {3, 4, 5} Difference: {1, 2}
Understanding these set properties and operations will enable you to effectively use sets for various data manipulation tasks in Python.
Verify with len() and Element Count
In this step, we will learn how to verify the properties of a set using the len() function and by checking for the presence of specific elements. The len() function returns the number of elements in a set, and we can use the in operator to check if an element is present in a set. These techniques are useful for validating the state of a set after performing operations.
Let's start by using the len() function to determine the size of a set:
Open the
set_example.pyfile in the~/projectdirectory using the VS Code editor.Modify the file to use the
len()function to print the number of elements in the set:## Create a set my_set = {1, 2, 3, 4, 5} ## Get the number of elements in the set set_length = len(my_set) ## Print the length of the set print("Length of the set:", set_length)Save the file.
Run the script using the
pythoncommand in the terminal:python set_example.pyYou should see the following output:
Length of the set: 5
Now, let's see how to check if an element is present in a set using the in operator:
Modify the
set_example.pyfile to check for the presence of specific elements:## Create a set my_set = {1, 2, 3, 4, 5} ## Check if an element is in the set if 3 in my_set: print("3 is in the set") else: print("3 is not in the set") ## Check if an element is not in the set if 6 in my_set: print("6 is in the set") else: print("6 is not in the set")Save the file.
Run the script again:
python set_example.pyYou should see the following output:
3 is in the set 6 is not in the set
By combining the len() function and the in operator, you can effectively verify the properties of a set and ensure that it contains the expected elements. This is particularly useful when working with sets in more complex programs where you need to validate the state of your data.
Summary
In this lab, you begin by learning about the fundamental concept of set uniqueness in Python. Sets are introduced as a built-in data type that stores unordered collections of unique elements, automatically eliminating duplicates. You create a Python script to demonstrate this property, observing that adding duplicate values to a set does not result in those duplicates being stored.
The lab then proceeds to delve deeper into the properties of sets, highlighting that they are more than just collections of unique elements. (The provided content is truncated, so this is based on the assumption that the next step builds upon the initial understanding of set uniqueness.)



