0% found this document useful (0 votes)
14 views7 pages

Motivation

This document discusses sets in Python, including their characteristics of unique elements and unordered nature, basic operations like adding, removing, and checking membership of elements, mathematical set operations like union and intersection, and relationships like subset and superset. Sets allow storing unique elements and performing operations efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Motivation

This document discusses sets in Python, including their characteristics of unique elements and unordered nature, basic operations like adding, removing, and checking membership of elements, mathematical set operations like union and intersection, and relationships like subset and superset. Sets allow storing unique elements and performing operations efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

4/25/24, 10:32 PM Notebook.

ipynb - Colab

keyboard_arrow_down Sets
Sets are an essential data structure in programming that allow you to store a collection of unique
elements. A set is an unordered collection, which means that the elements are not stored in any
particular order. The primary purpose of sets is to group related elements together and perform
various operations on them.

Motivation

Welcome to your journey of learning about sets in programming! Let's explore the key reasons
why learning about sets is valuable:

Uniqueness and Deduplication: Sets ensure that each element appears only once, making
them valuable for tasks such as removing duplicates from a collection or finding unique
elements in data. They help you work with distinct and non-repetitive data, which is
important for many programming tasks.

Efficient Membership Testing: Sets are super fast when it comes to checking if something
is part of the group or not, which makes them perfect when you need to quickly find out if
something is already in a group or not.

Mathematical Set Operations: Sets support various mathematical operations. You can
combine sets, compare them, or even take away some things from one set to make a new
one.

Characteristics of Sets

Sets have two primary characteristics:

Unique Elements: A set does not allow duplicate elements. Each element in a set is unique,
meaning that it appears only once. If you attempt to add a duplicate element to a set, it will
be ignored.
Unordered Nature: The elements in a set are not stored in a specific order. Unlike lists or
tuples, where the order of elements is significant, sets are not concerned with the
sequence of elements. This characteristic allows sets to be efficient in performing
operations like membership checks and eliminating duplicates.

keyboard_arrow_down Set Notation


https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/9. Sets/Notebook.ip… 1/7
4/25/24, 10:32 PM Notebook.ipynb - Colab

In Python, you can create sets using curly braces {} or the set() constructor.

Creating a set using the set() constructor looks like this:

my_set = set([1, 2, 3, 4, 4, 4, 6])


print(my_set)

Observe that in the above example that number 4 appears only once in the set, as an unique
entry.

Creating a set using curly braces looks like this:

my_set = {1, 2, 3, 4, 4, 4, 6}
print(my_set)

It's important to note that using curly braces {} without any elements will create
an empty dictionary instead of an empty set. To create an empty set, always use
the set() constructor.

Trying to index the set will result in a TypeError as elements don't have a specific
order in a set, as mentioned above.

my_set[1]

keyboard_arrow_down Basic Operations on Sets


keyboard_arrow_down Adding Elements to a Set
To add a single element to a set, use the add() method. For example:

fruits = {"grape", "strawberry", "blueberry"}


print(fruits)

fruits.add("apple")
print(fruits)

To add multiple elements to a set, use the update() method. For example:

fruits = {"grape", "strawberry", "blueberry"}


print(fruits)

fruits.update(["banana", "orange"])
print(fruits)

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/9. Sets/Notebook.ip… 2/7


4/25/24, 10:32 PM Notebook.ipynb - Colab

keyboard_arrow_down Removing Elements from a Set


To remove a specific element from a set, use the remove() method. For example:

print(fruits)

fruits.remove("banana")
print(fruits)

To remove and return an arbitrary element from a set, use the pop() method. For example:

print(fruits)

popped_element = fruits.pop()
print(popped_element)
print(fruits)

keyboard_arrow_down Checking Membership in a Set


To check if an element is/is not present in a set, use the in / not in keyword. For example:

print("apple" in fruits)
print("apple" not in fruits)

keyboard_arrow_down Finding the Length of a Set


We can retrieve the number of elements in a set using the len method, just like in a list:

my_set = {1, 5, 3, 6, 7, 5, 4, 5, 5, 5, 6}
len(my_set)

keyboard_arrow_down Finding the Minimum and Maximum in a Set


We can retrieve the minimum and maximum value in the set using the min and the max
functions:

print(min(my_set))
print(max(my_set))

keyboard_arrow_down Set Operations


https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/9. Sets/Notebook.ip… 3/7
4/25/24, 10:32 PM Notebook.ipynb - Colab

Sets in Python share the same principles as sets in mathematics and can support the same
operations. The most common ones are Union , Intersection , Difference , and Symmetric
Difference .

keyboard_arrow_down .union()

The union of two sets is a new set that contains all the unique elements from both sets. In
Python, you can find the union of sets using the union() method or the | operator. For example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set)
# Output: {1, 2, 3, 4, 5}

# Using the '|' operator:


union_set = set1 | set2
print(union_set)
# Output: {1, 2, 3, 4, 5}

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/9. Sets/Notebook.ip… 4/7


4/25/24, 10:32 PM Notebook.ipynb - Colab

keyboard_arrow_down .intersection()

The intersection of two sets is a new set that contains the common elements present in both
sets. In Python, you can find the intersection of sets using the intersection() method or the &
operator. For example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set)
# Output: {3}

# Using the '&' operator:


intersection_set = set1 & set2
intersection_set = set1.intersection(set2)
# Output: {3}

keyboard_arrow_down .difference()

The difference between two sets is a new set that contains the elements present in the first set
but not in the second set. In Python, you can find the difference between sets using the
difference() method or the - operator. For example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
print(difference_set)
# Output: {1, 2}

# Using the '-' operator:


difference_set = set1 - set2
print(difference_set)
# Output: {1, 2}

keyboard_arrow_down .symmetric_difference()

The symmetric difference between two sets is a new set that contains the elements that are
present in either of the sets but not in both. In Python, you can find the symmetric difference
between sets using the symmetric_difference() method or the ^ operator. For example:

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/9. Sets/Notebook.ip… 5/7


4/25/24, 10:32 PM Notebook.ipynb - Colab

set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set)
# Output: {1, 2, 4, 5}

# Using the '^' operator:


symmetric_difference_set = set1 ^ set2
print(symmetric_difference_set)
# Output: {1, 2, 4, 5}

keyboard_arrow_down Subset and Superset Relationships


In set theory, one set can be a subset or superset of another set. A set is considered a subset if
all its elements are present in the other set. Conversely, a set is considered a superset if it
contains all the elements of the other set. In Python, you can check subset and superset
relationships using the issubset() and issuperset() methods, which will return a Boolean
value. For example:

set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}

set1_subset_of_set2 = set1.issubset(set2)
print(set1_subset_of_set2)
# Output: True

set2_superset_of_set1 = set2.issuperset(set1)
print(set2_superset_of_set1)
# Output: True

keyboard_arrow_down Disjoint Sets


Two sets are considered disjoint if they have no common elements. In Python, you can check if
two sets are disjoint using the isdisjoint() method, which also returns a Boolean value. For
example:

set1 = {1, 2, 3}
set2 = {4, 5, 6}

are_disjoint = set1.isdisjoint(set2)
print(are_disjoint)
# Output: True

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/9. Sets/Notebook.ip… 6/7


4/25/24, 10:32 PM Notebook.ipynb - Colab

Key Takeaways

Sets in Python are an unordered collection of unique elements


Sets do not maintain any specific order of elements
Sets automatically eliminate duplicate elements
Sets can be created using curly braces {} or the set() constructor
Basic operations on sets include adding elements with add() and update() , removing
elements with remove() and pop() , checking membership with the in keyword, and
finding the length with len()
Set operations include finding the union of sets with union() or | , the intersection with
intersection() or & , the difference with difference() or - , the symmetric difference
with symmetric_difference() or ^ , and checking subset and superset relationships with
issubset() and issuperset()

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/9. Sets/Notebook.ip… 7/7

You might also like