Data structures. Sets. Unit 11.2А

1.

NIS PhM Kostanay
Data structures. Sets
Unit 11.2А: Data structures.
Programming teacher: Mr. Zelenov Boris
www.bzfar.org

2.

Learning objectives:
• 11.2.1.1 create sets;
• 11.2.1.2 use operations of adding, deleting, counting
the number of elements;
• 11.2.1.3 apply operations to sets: union,
intersection, subtraction, symmetric difference;
• 11.2.1.4 compare sets;
• 11.4.3.2 solve applied problems of various subject
areas.
www.bzfar.org

3.

Sets
• A set is an unordered collection of unique items.
does not contain identical elements;
the order of the elements in the set is not defined;
you can add and remove items;
elements of sets can be of different types;
you can iterate over the elements of the set;
you can check whether an element belongs to a set;
you can perform operations on sets (union, intersection, difference).
www.bzfar.org

4.

Creating Sets
# A set of strings
s = {'red', 'green', 'blue’}
# set fruits_and_price contains both string and
integer elements
fruits_and_price = {"apple", 300, "orange", 700,
"lemon", 900, "banana", 550}
www.bzfar.org

5.

Set Functions
numbers = {5, 3, 7, 4}
print(len(numbers)) # length of set fruits = 4
print (sum(numbers))
# the sum of the elements of the set (only for
numbers) = 19
print (min(numbers))
# minimum value of the set = 3
print (max(numbers))
# maximum value of the set = 7
www.bzfar.org

6.

Iteration of Sets
fruits = {"apple", "orange", "lemon", "banana"}
for item in fruits: # iterate over the elements of the set
print(item) # output of a set item, the order of output
may differ
Search for an item in a set
if item in my_set: # check if items are in the set
print('The item is in the set’)
else:
print('The item is not in set')
www.bzfar.org

7.

Add Items to Sets
S = {'red', 'green', 'blue'}
S.add('yellow')
print(S)
# Prints {'blue', 'green', 'yellow', 'red'}
my_set = set() # declare an empty set
my_set.add(1) # add the first item to the set
my_set.add(5) # add the second element to the set
my_set.add(1) # add the third element to the set
print(my_set) # will print a set of two elements {1, 5}
www.bzfar.org

8.

Deleting an item
discard - removes the given element, if it is in the set, and does
nothing if it is not;
my_set = {'one', 'two', 'three’}
my_set.discard('two') # element 'two' removed from set my_set
my_set.discard('four') # item not deleted, no error print(my_set) #
will print the remaining elements of the set {'one', 'three’}
remove - removes the given element, if any, and throws a KeyError if
not;
my_set = {'one', 'two', 'three’}
my_set.remove('two') # element 'two' removed from my_set
print(my_set) # will print the remaining elements of the set {'one',
'three’}
my_set.remove('four') # cannot be removed, KeyError
www.bzfar.org

9.

Deleting an item
pop - removes some element from the set
and returns it as a result. In this
case, the order of deletion is unknown.
my_set = {'one', 'two', 'three'}
print('before deletion:', my_set)
item = my_set.pop()
print('deleted item:', item)
print('after deletion:', my_set)
www.bzfar.org

10.

Clear set
my_set = {'one', 'two', 'three'}
my_set.clear()
print(my_set) # will print an empty set
set()
www.bzfar.org

11.

Set Operations
Set Union
A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}
# by operator
print(A | B)
# Prints {'blue', 'green', 'yellow', 'orange', 'red'}
# by method
print(A.union(B))
# Prints {'blue', 'green', 'yellow', 'orange', 'red'}
www.bzfar.org

12.

Set Operations
Set Intersection
A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}
# by operator
print(A & B)
# Prints {'red'}
# by method
print(A.intersection(B))
# Prints {'red'}
www.bzfar.org

13.

Set Operations
Set Difference
A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}
# by operator
print(A - B)
# Prints {'blue', 'green'}
# by method
print(A.difference(B))
# Prints {'blue', 'green'}
www.bzfar.org

14.

Set Operations
Set Symmetric Difference
A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}
# by operator
print(A ^ B)
# Prints {'orange', 'blue', 'green', 'yellow'}
# by method
print(A.symmetric_difference(B))
# Prints {'orange', 'blue', 'green', 'yellow'}
www.bzfar.org

15.

Solving tasks
• Stepik.org
• 5.1
www.bzfar.org
English     Русский Правила