Programming-1 Using Python
Dr. Yasmin Hosny Alkady
Faculty of Information Technology & Computer Science
Sinai University
E-Mail: yassmin.hosny@su.edu.eg
Programming-1
Using Python
Lecture 4
Sets & Dictionary
3
Outlines
1) Python Sets
2) Python Dictionaries
4
Python Collections (Arrays)
▰ There are four collection data types in the Python programming language:
list Tuple Set Dictionary
5
1
Python Sets
6
Sets
▰ Sets are used to store multiple items in a single variable.
▰ Set is one of 4 built-in data types in Python used to store collections of data, the
other 3 are set, Tuple, and Dictionary, all with different qualities and usage.
▰ Sets are written with curly brackets { }.
▰ Example: Create a Set
set1={1,2,3}
print (set1)
7
Set Items
▰ Set items are unordered, unchangeable, and do not allow duplicate values.
▰ Unordered: means that the items in a set do not have a defined order, cannot be
referred to by index or key.
▰ Unchangeable: meaning that we cannot change the items after the set has been
created.
▰ Duplicates Not Allowed: Sets cannot have two items with the same value.
8
Set Length
▰ To determine how many items a set has, use the len( ) function:
▰ Example: Print the number of items in the set
set1={"banan",2,"apple“}
print( len (set1) )
9
Set Items - Data Types
▰ Set items can be of any data type:
▰ A set with strings, integers and boolean values:
▰ Example:
set1 = {"apple", "banana", "cherry“}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
set4= {"apple", 1, True}
▰ To get the type
set1={"banan",2,"apple“}
print(type(set1))
10
Access Set Items
▰ You cannot access items in a set by referring to an index or a key.
▰ Example: Check if "banana" is present in the set:
set = {"banan", 2,"apple"}
print ("banan" in set)
▰ Once a set is created, you cannot change its items, but you can add new items.
11
Check if Item Exists
▰ To determine if a specified item is present in a set use the in keyword
▰ Example: Check if "apple" is present in the set:
set = {"apple", "banana", "cherry“}
if "apple" in set:
print("Yes, 'apple' is in the fruits set")
12
Add Sets
▰ To add items from another set into the current set, use the update( ) method.
▰ Example: Add elements from set1 and set2 into set1:
set1 = {"apple", "banana", "cherry"}
set2={10, 20, 30, 40}
set1.update(set2)
print(set1)
13
Remove Set Items
▰ To remove an item in a set, use the remove( ), or the discard( ) method.
▰ Example: Remove 40 by using the remove( ) or discard( ) method:
x={10, 20, 30, 40, 50, 60} x={10, 20, 30, 40, 50, 60}
x.remove(40) x.discard(40)
print(x) print(x)
▰ Note: If the item to remove does not exist, remove() will raise an error.
14
Remove Set Items
▰ You can also use the pop( ) method to remove an item, but this method will remove the last
item. Remember that sets are unordered, so you will not know what item that gets removed.
▰ Example:
x={10, 20, 30, 40, 50, 60}
y = x. pop( )
print(y)
print(x)
▰ Note: Sets are unordered, so when using the pop() method, you do not know which item that
gets removed.
15
Remove all items in set (to be empty set)
Example: The clear( ) method empties the set:
x={10, 20, 30, 40, 50, 60}
x.clear()
print(x)
16
Delete the set completely
▰ The del keyword will delete the set completely:
▰ Example:
x={10, 20, 30, 40, 50, 60}
del x
17
Join Two Sets
▰ There are several ways to join two or more sets in Python. Both union( ) and update( ) will join
two sets and exclude any duplicate items
▰ Example: Add the elements of y to x:
x = {"apple", "banana", "cherry"} x = {"apple", "banana", "cherry"}
y = {"mango", "pineapple", "papaya", ,"banana"} y = {"mango", "pineapple", "papaya", ,"banana"}
z= x.union(y) x.update(y)
print(z) print(x)
18
Keep ONLY the Duplicates
▰ The intersection-update() method will keep only the items that are present in both sets.
▰ Example: Keep the items that exist in both set x, and set y:
x = {"apple", "banana", "cherry"}
y = {"mango", "pineapple", "papaya", ,"banana"}
x. intersection_update(y)
print(x)
19
Keep ONLY the Duplicates
▰ The intersection() method will return a new set, that only contains the items that are present in
both sets
▰ Example: Keep the items that exist in both set x, and set y:
x = {"apple", "banana", "cherry"}
y = {"mango", "pineapple", "papaya", ,"banana"}
z= x. intersection(y)
print(z)
20
Keep All, But NOT the Duplicates
▰ The symmetric-difference-update( ) method will keep only the elements that are NOT present in
both sets.
▰ Example: Keep the items that are not present in both sets:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.symmetric_difference_update(y)
print(x)
21
Keep All, But NOT the Duplicates
▰ The symmetric-difference( ) method will return a new set, that contains only the elements that
are NOT present in both sets.
▰ Example: Keep the items that are not present in both sets:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z= x.symmetric_difference (y)
print(z)
22
2
Python Dictionaries
23
Dictionaries
▰ Dictionaries are used to store data values in key: value pairs.
▰ A dictionary is a collection which is unordered, changeable and does not allow
duplicates.
▰ Dictionaries are written with curly brackets, and have keys and values
my_dictionary={"brand":"opel",
"model":"grand land",
"year":2021}
print(my_dictionary)
24
Dictionary Items
▰ Dictionary items are ordered, changeable, and does not allow duplicates.
▰ Dictionary items are presented in key:value pairs, and can be referred to by using the key
name.
▰ Example: Print the "brand" value of the dictionary:
my_dictionary={"brand":"opel",
"model":"grand land",
"year":2021}
print(my_dictionary["brand"])
25
Dictionary Items
▰ Dictionary items are ordered, changeable, and does not allow duplicates.
▰ Duplicates Not Allowed: Dictionaries cannot have two items with the same key
▰ Example: Duplicate values will overwrite existing values:
my_dictionary={"brand":"opel",
"model":"grand land",
"year":2021,
"year":2020}
print(my_dictionary)
26
Dictionary Length
▰ To determine how many items a dictionary has, use the len( ) function:
▰ Example: Print the number of items in the dictionary:
my_dictionary={"brand":"opel",
"model":"grand land",
"year":2021}
print(len(my_dictionary))
27
Dictionary Items - Data Types
▰ Dictionary items can be of any data type (String, int and boolean)
▰ Example: String, int, boolean, and list data types:
my_dictionary={"brand":"opel",
"model":"grand land",
"Fuel": True,
"year":2021}
28
type()
▰ Example: String, int, boolean, and list data types:
my_dictionary={"brand":"opel",
"model":"grand land",
"Fuel": True,
"year":2021}
print(type(my_dictionary))
29
Access Dictionary Items
▰ You can access dictionary items by referring to the key, inside square brackets:
my_dictionary={"brand":"opel",
"model":"grand land",
"Fuel": True,
"year":2021}
x= (my_dictionary["model"])
print(x)
30
Access Dictionary Items(another way)
▰ There is also a method called get( ) that will give you the same result
my_dictionary={"brand":"opel",
"model":"grand land",
"Fuel": True,
"year":2021}
x= (my_dictionary.get("model"))
print(x)
31
Get Keys
▰ The keys( ) method will return a list of all the keys in the dictionary.
▰ Example: Get a list of the keys
my_dictionary={"brand":"opel",
"model":"grand land",
"Fuel": True,
"year":2021}
x= (my_dictionary.keys())
print(x)
32
Adding new Key
my_dictionary={"brand":"opel",
"model":"grand land",
"Fuel": True,
"year":2021}
x= (my_dictionary.keys())
#keys before adding new key
print(x)
my_dictionary["color"]="Black"
#keys after adding new key
print(x)
33
Get Values
▰ The values( ) method will return a list of all the values in the dictionary
▰ Example: Get a list of the values
my_dictionary={"brand":"opel",
"model":"grand land",
"Fuel": True,
"year":2021}
x= (my_dictionary.values())
print(x)
34
Get Values
car={
"type": "cedan",
"brand": "kia",
"model": "cerato",
"year": 2019
}
x=car.values()
print(x)
car["fuel"]= True
print(x)
35
Get Items
▰ The items( ) method will return each item in a dictionary, as tuples in a list
▰ Example: Get a list of the items
car={
"type": "cedan",
"brand": "kia",
"model": "cerato",
"year": 2019
}
x=car.items()
print(x)
36
Get Items
car={
"type": "cedan",
"brand": "kia",
"model": "cerato",
"year": 2019
}
x=car.items()
print(x) #before change
car["fuel"]=True
print(x) #after change
37
Check if Key Exists
▰ To determine if a specified key is present in a dictionary use the in keyword:
car={
"type": "cedan",
"brand": "kia",
"model": "cerato",
"year": 2019
}
if "brand" in car:
print("hima is a good boy")
38
Change Dictionary Items
▰ You can change the value of a specific item by referring to its key name:
▰ Example: Change the “brand" to “ Honda":
car={
"type": "cedan",
"brand": "kia",
"model": "cerato",
"year": 2019
}
car["brand"]="Honda"
print(car)
39
Update Dictionary
▰ update( ) method will update the dictionary with the items from the given argument.
▰ You can use update to add a new item
▰ Example: Update the "year" of the car by using the update( ) method:
car={ "type": "cedan", "brand": "kia","model": "cerato","year": 2019 }
car.update({"fuel":True})
print(car)
40
Removing Items
▰ The pop( ) method removes the item with the specified key name:
car={
"type": "cedan",
"brand": "kia",
"model": "cerato",
"year": 2019
}
car.pop("year")
print(car)
41
Removing Items
▰ The del keyword removes the item with the specified key name:
car={
"type": "cedan",
"brand": "kia",
"model": "cerato",
"year": 2019
}
del car["year"]
print(car)
42
Removing Dictionary completely
▰ The del keyword can also delete the dictionary completely:
car={
"type": "cedan",
"brand": "kia",
"model": "cerato",
"year": 2019
}
del car
print(car)
43
clear Dictionary (empty)
▰ The clear( ) method empties the dictionary:
car={"type": "cedan","brand": "kia","model": "cerato","year": 2019}
car.clear()
print(car)
44
Copy Dictionaries
▰ Make a copy of a dictionary with the copy( ) method:
car={"type": "cedan","brand": "kia","model": "cerato","year": 2019}
new_car=car.copy()
print(new_car)
45
THANK YOU
46