msbacademy.org
Python
Dictionaries and Sets
msbacademy.org
Dictionary
1. A dictionary is an object that stores a collection of data.
2. Each element in a dictionary has two parts: a key and a value.
3. You use a key to locate a specific value.
msbacademy.org
Creating a Dictionary
Colon
msb_dict = {‘students':‘60', ‘systems':‘30'}
dictionary_name Key value Comma
msbacademy.org
Retrieving a Value
To retrieve a value from a dictionary, you simply write an
expression in the following general format
Syntax:
dictionary_name[key]
msbacademy.org
Test for a Value
Using the in and not in Operators to Test for a Value in Dictionary
Ex: msb_dict = {'students':'60', 'systems':'30', 'chairs':'90'}
if 'students‘ in msb_dict:
print(msb_dict['students'])
msbacademy.org
Adding Elements
Adding Elements to an Existing Dictionary
Ex:
msb_dict = {'students':'60', 'systems':'30', 'chairs':'90'}
msb_dict['courses'] = '20'
msb_dict['students'] = '50'
NOTE: You cannot have duplicate keys in a dictionary. When you
assign a value to an existing key, the new value replaces the
existing value.
msbacademy.org
Deleting Elements
You can delete an existing key-value pair from a dictionary
with the del statement. Here is the general format:
Syntax:
del dictionary_name[key]
msbacademy.org
Getting the Number of Elements
You can use the built-in “len” function to get the number of
elements in a dictionary
msbacademy.org
Mixing Data Types
The keys in a dictionary must be immutable objects, but their
associated values can be any type of object
msbacademy.org
Creating an Empty Dictionary
Sometimes you need to create an empty dictionary and then add
elements to it as the program executes. You can use an empty
set of curly braces to create an empty dictionary
msbacademy.org
Using the for loop to Iterate over
Using the for loop to Iterate over a Dictionary
You can use the for loop in the following general format to iterate
over all the keys in a dictionary
for var in dictionary:
statement
statement
etc.
msbacademy.org
Some Dictionary Methods
1. Clear
2. Get
3. Items
4. Keys
5. Pop
6. Popitem
7. values
msbacademy.org
Clear method
The clear method deletes all the elements in a dictionary,
leaving the dictionary empty. The method’s general format is
Syntax:
dictionary.clear()
msbacademy.org
Get method
You can use the get method as an alternative to the [ ]
operator for getting a value from a dictionary.
Syntax:
dictionary.get(key, default)
msbacademy.org
Items method
The items method returns all of a dictionaries keys and
their associated values. They are returned as a special type of
sequence known as a dictionary view.
Syntax:
dictionary.items()
msbacademy.org
Keys method
The keys method returns all of a dictionaries keys as a
dictionary view, which is a type of sequence. Each element in the
dictionary view is a key from the dictionary.
Syntax:
dictionary.key()
msbacademy.org
Pop method
The pop method returns the value associated with a
specified key and removes that key value pair from the dictionary.
If the key is not found, the method returns a default value
Syntax:
dictionary.pop(key, default)
msbacademy.org
Popitem method
The popitem method returns a randomly selected key-value
pair, and it removes that key value pair from the dictionary. The
key-value pair is returned as a tuple
Syntax: dictionary.popitem()
You can use an assignment statement in the following
general format to assign the returned key and value to individual
variables
Syntax: k, v = dictionary.popitem()
msbacademy.org
Set
A set is an object that stores a collection of data in the same way
as mathematical sets.
1. All the elements in a set must be unique. No two elements
can have the same value.
2. Sets are unordered, which means that the elements in a set
are not stored in any particular order.
3. The elements that are stored in a set can be of different data
types
msbacademy.org
Creating a Set
To create a set, you have to call the built-in set function
Ex:
msb_set = set(['a', 'b', 'c'])
msbacademy.org
Getting the Number of Elements
As with lists, tuples, and dictionaries, you can use the “len”
function to get the number of elements in a set
msbacademy.org
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
msbacademy.org
‘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:
statement
statement
etc.
msbacademy.org
Finding the Union of Sets
The union of two sets is a set that contains all the elements
of both sets. In Python, you can call the union method to get the
union of two sets
Syntax:
set1.union(set2)
msbacademy.org
Finding the Intersection of Sets
The intersection of two sets is a set that contains only the
elements that are found in both sets. In Python, you can call the
intersection method to get the intersection of two sets.
Syntax:
set1.intersection(set2)
msbacademy.org
Finding the Difference of Sets
The difference of set1 and set2 are the elements that
appear in set1 but do not appear in set2. In Python, you can call
the difference method to get the difference of two sets
Syntax:
set1.difference(set2)
msbacademy.org
Finding the Symmetric Difference of Sets
The symmetric difference of two sets is a set that contains
the elements that are not shared by the sets. In other words, it is
the elements that are in one set but not in both. In Python, you
can call the symmetric_difference method to get the symmetric
difference of two sets
Syntax:
set1.symmetric_difference(set2)
msbacademy.org
Finding Subsets and Supersets
Suppose you have two sets and one of those sets contains all of
the elements of the other set
Ex: set1 = set([1, 2, 3, 4])
set2 = set([2, 3])
set2.issubset(set1)
set1.issuperset(set2)
msbacademy.org
Follow Us:
/msbacademy
/msb academy
/msb_academy
/msb_academy
Thank You

Dictionaries and Sets in Python

  • 1.
  • 2.
    msbacademy.org Dictionary 1. A dictionaryis an object that stores a collection of data. 2. Each element in a dictionary has two parts: a key and a value. 3. You use a key to locate a specific value.
  • 3.
    msbacademy.org Creating a Dictionary Colon msb_dict= {‘students':‘60', ‘systems':‘30'} dictionary_name Key value Comma
  • 4.
    msbacademy.org Retrieving a Value Toretrieve a value from a dictionary, you simply write an expression in the following general format Syntax: dictionary_name[key]
  • 5.
    msbacademy.org Test for aValue Using the in and not in Operators to Test for a Value in Dictionary Ex: msb_dict = {'students':'60', 'systems':'30', 'chairs':'90'} if 'students‘ in msb_dict: print(msb_dict['students'])
  • 6.
    msbacademy.org Adding Elements Adding Elementsto an Existing Dictionary Ex: msb_dict = {'students':'60', 'systems':'30', 'chairs':'90'} msb_dict['courses'] = '20' msb_dict['students'] = '50' NOTE: You cannot have duplicate keys in a dictionary. When you assign a value to an existing key, the new value replaces the existing value.
  • 7.
    msbacademy.org Deleting Elements You candelete an existing key-value pair from a dictionary with the del statement. Here is the general format: Syntax: del dictionary_name[key]
  • 8.
    msbacademy.org Getting the Numberof Elements You can use the built-in “len” function to get the number of elements in a dictionary
  • 9.
    msbacademy.org Mixing Data Types Thekeys in a dictionary must be immutable objects, but their associated values can be any type of object
  • 10.
    msbacademy.org Creating an EmptyDictionary Sometimes you need to create an empty dictionary and then add elements to it as the program executes. You can use an empty set of curly braces to create an empty dictionary
  • 11.
    msbacademy.org Using the forloop to Iterate over Using the for loop to Iterate over a Dictionary You can use the for loop in the following general format to iterate over all the keys in a dictionary for var in dictionary: statement statement etc.
  • 12.
    msbacademy.org Some Dictionary Methods 1.Clear 2. Get 3. Items 4. Keys 5. Pop 6. Popitem 7. values
  • 13.
    msbacademy.org Clear method The clearmethod deletes all the elements in a dictionary, leaving the dictionary empty. The method’s general format is Syntax: dictionary.clear()
  • 14.
    msbacademy.org Get method You canuse the get method as an alternative to the [ ] operator for getting a value from a dictionary. Syntax: dictionary.get(key, default)
  • 15.
    msbacademy.org Items method The itemsmethod returns all of a dictionaries keys and their associated values. They are returned as a special type of sequence known as a dictionary view. Syntax: dictionary.items()
  • 16.
    msbacademy.org Keys method The keysmethod returns all of a dictionaries keys as a dictionary view, which is a type of sequence. Each element in the dictionary view is a key from the dictionary. Syntax: dictionary.key()
  • 17.
    msbacademy.org Pop method The popmethod returns the value associated with a specified key and removes that key value pair from the dictionary. If the key is not found, the method returns a default value Syntax: dictionary.pop(key, default)
  • 18.
    msbacademy.org Popitem method The popitemmethod returns a randomly selected key-value pair, and it removes that key value pair from the dictionary. The key-value pair is returned as a tuple Syntax: dictionary.popitem() You can use an assignment statement in the following general format to assign the returned key and value to individual variables Syntax: k, v = dictionary.popitem()
  • 19.
    msbacademy.org Set A set isan object that stores a collection of data in the same way as mathematical sets. 1. All the elements in a set must be unique. No two elements can have the same value. 2. Sets are unordered, which means that the elements in a set are not stored in any particular order. 3. The elements that are stored in a set can be of different data types
  • 20.
    msbacademy.org Creating a Set Tocreate a set, you have to call the built-in set function Ex: msb_set = set(['a', 'b', 'c'])
  • 21.
    msbacademy.org Getting the Numberof Elements As with lists, tuples, and dictionaries, you can use the “len” function to get the number of elements in a set
  • 22.
    msbacademy.org Adding and RemovingElements 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
  • 23.
    msbacademy.org ‘Iterate’ over aSet You can use the for loop in the following general format to iterate over all the elements in a set: for var in set: statement statement etc.
  • 24.
    msbacademy.org Finding the Unionof Sets The union of two sets is a set that contains all the elements of both sets. In Python, you can call the union method to get the union of two sets Syntax: set1.union(set2)
  • 25.
    msbacademy.org Finding the Intersectionof Sets The intersection of two sets is a set that contains only the elements that are found in both sets. In Python, you can call the intersection method to get the intersection of two sets. Syntax: set1.intersection(set2)
  • 26.
    msbacademy.org Finding the Differenceof Sets The difference of set1 and set2 are the elements that appear in set1 but do not appear in set2. In Python, you can call the difference method to get the difference of two sets Syntax: set1.difference(set2)
  • 27.
    msbacademy.org Finding the SymmetricDifference of Sets The symmetric difference of two sets is a set that contains the elements that are not shared by the sets. In other words, it is the elements that are in one set but not in both. In Python, you can call the symmetric_difference method to get the symmetric difference of two sets Syntax: set1.symmetric_difference(set2)
  • 28.
    msbacademy.org Finding Subsets andSupersets Suppose you have two sets and one of those sets contains all of the elements of the other set Ex: set1 = set([1, 2, 3, 4]) set2 = set([2, 3]) set2.issubset(set1) set1.issuperset(set2)
  • 29.