6Dictionaries and sets in pythonsss.pptx

OkelloBenjamin2 22 views 21 slides Sep 24, 2024
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

Skip to primary navigation
Skip to main content
Skip to footer
Cisco Umbrella

Enterprise network security
Watch on-demand
Cisco Umbrella Demo
Simplify and strengthen cloud security for your security service edge (SSE) journey

Learn how you can boost your security posture by reducing your risk expo...


Slide Content

Dictionaries and Sets

Dictionaries An object that stores a collection of data. Each element in a dictionary has two parts: a key and a value . You use a key to locate a specific value Often referred to as mapping of key to value The values in a dictionary can be objects of any type Key must be an immutable object Format phonebook = {'Chris':'555-1111', 'Katie':'555-2222', 'Joanne':'555-3333'}

Retrieving a Value from a Dictionary >>> phonebook = {'Chris':'555-1111', 'Katie':'555-2222', 'Joanne':'555-3333'} >>> phonebook {'Chris': '555-1111', 'Joanne': '555-3333', 'Katie': '555-2222'} To retrieve a value from a dictionary, you simply write an expression in the following general format: dictionary_name [key] >>> phonebook = {'Chris':'555-1111', 'Katie':'555-2222', 'Joanne':'555-3333'} e >>> phonebook['Chris'] 3 '555-1111'

Using the in and not in Operators to Test for a Value in a Dictionary >>> phonebook = {'Chris':'555-1111', 'Katie':'555-2222', 'Joanne':'555-3333'} >>> if 'Chris' in phonebook: print(phonebook['Chris']) 5 555-1111 >>> phonebook = {'Chris':'555-1111', 'Katie':'555-2222'} >>> if 'Joanne' not in phonebook: print('Joanne is not found.') Joanne is not found .

Adding Elements to an Existing Dictionary Dictionaries are mutable objects To add a new key-value pair: dictionary [ key ] = value If key exists in the dictionary, the value associated with it will be changed

Deleting Elements From an Existing Dictionary To delete a key-value pair: del dictionary [ key ] If key is not in the dictionary, KeyError exception is raised To avoid errors, use the in and not in Operators to Test for a Value in a Dictionary

Getting the Number of Elements and Mixing Data Types len function : used to obtain number of elements in a dictionary Keys must be immutable objects, but associated values can be any type of object One dictionary can include keys of several different immutable types Values stored in a single dictionary can be of different types

Creating an Empty Dictionary and Using for Loop to Iterate Over a Dictionary To create an empty dictionary: Use {} Use built-in function dict () Elements can be added to the dictionary as program executes Use a for loop to iterate over a dictionary General format: for key in dictionary :

Some Dictionary Methods

Some Dictionary Methods Expl’d Clear method get method : gets a value associated with specified key from the dictionary Format: dictionary .get ( key , default ) default is returned if key is not found

Some Dictionary Methods Expl’d items method: returns all the dictionaries keys and associated values. Format: dictionary .items () keys method : returns all the dictionaries keys as a sequence Format: dictionary .keys ()

Some Dictionary Methods Expl’d pop method : returns value associated with specified key and removes that key-value pair from the dictionary Format: dictionary .pop ( key , default ) default is returned if key is not found popitem method : returns a randomly selected key-value pair and removes that key-value pair from the dictionary Format: dictionary .popitem () values method : returns all the dictionaries values as a sequence Format: dictionary .values () Use a for loop to iterate over the values

S ets Set : object that stores a collection of data in same way as mathematical set All items must be unique Set is unordered Elements can be of different data types

Creating a Set myset = set() – creates empty set myset = set(['a', 'b', 'c']) - passing a list in set arguments myset = set(' abc ') – each element is unique a, b, c Sets don’t contain duplicates E.g myset = set(' aaabc ') prints a,b,c

Abt sets This doesn’t work This Works

Abt sets Getting number of elements in a set Adding and removing elements

Abt sets Using the forLoop to Iterate over a Set Using the in and not in Operators to Test for a Value in a Set

Finding the Union of Sets

Finding the Intersection of Sets

Others on sets Finding the Difference of Sets Finding the Symmetric Difference of Sets Finding Subsets and Supersets

Sets Class Task In this section you will write a program which demonstrates various set operations. The program creates two sets: one that holds the names of students on the baseball team and another that holds the names of students on the basketball team. The program then performs the following operations: It finds the intersection of the sets to display the names of students who play both sports. It finds the union of the sets to display the names of students who play either sport. It finds the difference of the baseball and basketball sets to display the names of students who play baseball but not basketball. It finds the difference of the basketball and baseball (basketball– baseball) sets to display the names of students who play basketball but not baseball. It also finds the difference of the baseball and basketball (baseball– basketball) sets to display the names of students who play baseball but not basketball