Search Header Logo
Summer Lesson 23

Summer Lesson 23

Assessment

Presentation

Computers

9th - 12th Grade

Practice Problem

Hard

Created by

Robert Giordano

FREE Resource

17 Slides • 5 Questions

1

media

Lesson 23

Sets

2

media

Data Structures

So far we have looked at lists and tuples. These 2 data
structures each have a distinct usefulness as we
discussed. Let’s take a quick look at them to recap:

List: Ordered, changeable, allows duplicates

Tuple: Ordered, unchangeable, allows duplicates

Today we will discuss sets the 3rd of 4 data structures
available in Python

3

media

Sets

A set is a data structure in Python that is defined by
using curly braces { }. The items in a set, like with a
tuple and a list are separated by commas inside the
braces.

Sets are unordered, unchangeable*, unindexed, and do
not allow duplicate values.

Unordered: The items in a set are not kept in any
specific order. This means that they do not have
indexes for each element either which is why they are
unindexed.

Unchangeable*: The items in a set cannot be changed,
however, we can add and remove items from a set, we
simply cannot change the value of an item in the set.

4

Multiple Choice

Which of these denotes a set

1

( )

2

[ ]

3

{ }

4

< >

5

media

Declaring a Set

Like with lists and tuples, we can declare a set by
giving it a name and filling the { } with the values of the
set. Like with lists, we can also set up a blank set and
add values to it later.

6

media
media

Example:

7

Multiple Choice

True/False:

The order you enter values into a set is the same order they will always be in.

1

True

2

False

8

media

Looping Through a
Set

Since sets are unordered and unindexed it might seem
like looping through might be difficult. However, the for
loop still works, the difference is that it doesn’t use
any indexes to find each element. This also means
that the order that the elements will be accessed is
not guaranteed to be the same each time we loop
through.

9

media
media
media
media

Example:

10

Drag and Drop

Complete the code to print the numbers in the set:



mySet = {1, 8 ,3, 4, 9, 2, 6}

for ​
in ​
:

print(​
)
Drag these tiles and drop them in the correct blank above
num
mySet
range(len(mySet))
x
mySet[x]

11

media

Length of a Set

Like with lists and tuples, we can use the len() function
to determine the number of items in a set.

12

media
media
media

Example:

13

media

Printing a Set

We saw that we can use a loop to iterate through and
print each element. We can also print the set the same
way we printed lists and tuples.

Note* repl printed the set in the same order every time
it was run. PyCharm printed a different order each
time.

14

media
media
media

Example

15

media

Adding Elements to
a Set

We can use the add() method to add an element to a
set. Because sets are unordered, there’s no way of
knowing where the item will be placed in the set.

16

media
media
media

Example:

17

Fill in the Blank

Fill in the blank to add the number 12 to the set

mySet = {1, 8, 9, 15, 5}

mySet._____________

(
)

18

media

Removing Elements
from a Set

There are multiple methods to remove an element
from a set.

1.pop() - removes the last element from the set.
Since sets are unordered, there’s no way of
knowing which element will be removed

2.remove(value) - removes the element that
matches the value. If no element exists in the
set that matches, an error will be produced.

3.discard(value) - removes the element that
matches the value. If no element exists in the
set that matches, it will not produce an error.

19

media
media
media

Example:

20

Match

Match the following

pop( )

remove( )

discard( )

Removes the last element of the set.

removes the element that matches the value. Causes an error if no value matches

removes the element that matches the value. Does not cause an error if there is no matching value.

21

media

Emptying or
Deleting a Set

It is also possible to empty a set using the clear()
method. A set can also be deleted using the del
keyword.

22

media
media
media

Example:

media

Lesson 23

Sets

Show answer

Auto Play

Slide 1 / 22

SLIDE