You are on page 1of 14

11/28/2021

www.tvtc.gov.sa

dictionary and sets


Pearson Education , Edited by :Nouf Almunyif

Dictionaries

Pearson Education , Edited by :Nouf Almunyif 2

1
11/28/2021

Dictionaries
• Dictionaries are used to store data values in
key : value pairs.
• A dictionary is a collection which is ordered, changeable
and does not allow duplicates.
• Creating a dictionary is as simple as placing items inside curly braces {}
separated by commas.
• An item has a key and a corresponding value that is expressed as a pair
(key: value).

Pearson Education , Edited by :Nouf Almunyif 3

Dictionaries

Pearson Education , Edited by :Nouf Almunyif 4

2
11/28/2021

Pearson Education , Edited by :Nouf Almunyif 5

Accessing Elements from Dictionary

• You can access the items of a dictionary by referring to its


key name, inside square brackets:

• Keys can be used with the get() method.

• Trying to access keys which doesn't exist throws error

Pearson Education , Edited by :Nouf Almunyif 6

3
11/28/2021

Using the in and not in Operators to Test


for a Value in a Dictionary

Pearson Education , Edited by :Nouf Almunyif 7

Pearson Education , Edited by :Nouf Almunyif 8

4
11/28/2021

Changing and Adding Dictionary elements

Pearson Education , Edited by :Nouf Almunyif 9

Getting the Number of Elements in a Dictionary

You can use the built-in len function to get the number of elements in a
dictionary

Pearson Education , Edited by :Nouf Almunyif 10

5
11/28/2021

Mixing Data Types in a Dictionary

Pearson Education , Edited by :Nouf Almunyif 11

Pearson Education , Edited by :Nouf Almunyif 12

6
11/28/2021

Using the for Loop to Iterate over a Dictionary

Pearson Education , Edited by :Nouf Almunyif 13

Some Dictionary Methods

Pearson Education , Edited by :Nouf Almunyif 14

7
11/28/2021

Dictionary Methods
• The keys() method will return a list of all the keys in the dictionary.
• The values() method will return a list of all the values in the
dictionary.

Pearson Education , Edited by :Nouf Almunyif 15

Dictionary Methods

Pearson Education , Edited by :Nouf Almunyif 16

8
11/28/2021

SETS

Pearson Education , Edited by :Nouf Almunyif 17

sets

Pearson Education , Edited by :Nouf Almunyif 18

9
11/28/2021

Creating a Set

Pearson Education , Edited by :Nouf Almunyif 19

Creating a Set

Pearson Education , Edited by :Nouf Almunyif 20

10
11/28/2021

Creating a Set

For example, how would you create a set containing the elements 'one', 'two', and 'three'?
The following code does not accomplish the task, because you can pass no more than one
argument to the set function:

Pearson Education , Edited by :Nouf Almunyif 21

Getting the Number of Elements in a Set


As with lists, tuples, and dictionaries, you can use the len function to
get the number of elements in a set

Pearson Education , Edited by :Nouf Almunyif 22

11
11/28/2021

Adding and Removing Elements


• Sets are mutable objects, so you can add items to them and remove
items from them. You use the add method to add an element to a set.

Pearson Education , Edited by :Nouf Almunyif 23

Adding and Removing Elements


• You can add a group of elements to a set all at one time with the
update method.

Pearson Education , Edited by :Nouf Almunyif 24

12
11/28/2021

Adding and Removing Elements


• You can remove an item from a set with either the remove method or the discard method.
• The only difference between the two methods is how they behave when the specified item is not
found in the set. The remove method raises a KeyError exception, but the discard method does not
raise an exception

Pearson Education , Edited by :Nouf Almunyif 25

Using the for Loop to Iterate over a Set


• You can use the for loop in the following general format to iterate over all the
elements
• in a set:
for var in set: You can use in and not in Operators to Test
for a Value in a Set
statement
statement
etc.

Pearson Education , Edited by :Nouf Almunyif 26

13
11/28/2021

‫العنوان الفرعي‬

Pearson Education , Edited by :Nouf Almunyif 27

14

You might also like