Dictionary and Sets Flashcards
What is a dictionary?
An object that stores a collection of data.
A dictionary has two parts. What are they?
key
value
How can you locate a specific value
using key.
Webster analogy
words = keys definition = values
Key-Value pairs are often referred as?
Mappings
Why is key-value pairs referred as mappings?
Because each key is mapped to a value
Create a simple dictionary and explain..
lastname = {‘Ryan’: ‘Higa’, ‘Hanna’ : ‘Martin’, ‘Justin : Timber’}
»> lastname
»> lastname [‘Ryan’] –ryan = key
then it gives u value lastname : Higa.
Keys must be what?
immutable objects. cannot be lists
Syntax for adding new elements
phonebook[‘Sakura’] = ‘Tokyo, Japan’
Can you have duplicate keys in the dictionary?
Nope. Even if you do it will just replace.
Syntax for deleting?
del phonebook[“Sakura”]
Syntax for checking if sakura is in the dictionary
if ‘Sakura’ not in phonebook
print(“Sorry not in the set”)
if ‘Sakura’ in phonebook
print(“In the phonebook”)
How do you get the number of elements in a dictionary?
using len function.
Eg. len(phonebook)
How do you create an empty dictionary?
dictionaryname = {} or dict()
syntax for using loop and printing something in a dictionary
for key in phonebook:
print(key) #for key
print(phonebook[key]) # for value
What does the clear method do?
It clears all the elements of the dictionary
phonebook.clear()
What does the get method do?
gets a value from the dictionary.
Will it raise an exception is key is not found?
No, the method returns default.
How to print everything in dictionary
phonebook.items()
How to print only the keys
phonebook.keys()
how to print only the values
phonebook.values()
What is a set?
A set is a collection of unique values and works like a mathematical set.
Name the rules of set.
All the elements in the set must be unique
they are unordered.
the elements can be of different data types.
How do you create a set?
must use a built in function
myset = set()
How do you create an empty set?
myset = set()
How do you create a set with a,b,c?
myset = set([‘a’,’b’,’c’])
What happens if you create a set like this : myset(‘aabbc’)
well the repeating wont be in a set. Instead just this : a,b,c
How to get the number of elements in the set?
len(myset)
How to add 1 and 2 to myset?
myset. add(1)
myset. add(2)
How to add 4,5,6,7,8,9 to myset?
myset.update([4,5,6,7,8,9)]
How to remove a, b and c from myset?
myset. remove(‘a’)
myset. remove(‘b’)
myset. remove(‘c’)
How to remove all the items from myset?
myset.clear()
How to print all the elements from myset using loops (for loop)?
for count in myset:
print (count)
newset = set([1,2,3])
Check if 1 is in the newset and print if found
if 1 in newset:
print(“Found”)
newset = set([1,2,3])
Check if 7 is in the newset and print if not found
if 7 not in newset:
print(“Not found”)
Find the union of the following sets:
set1=set([1,2,3])
set2=set([3,4,5])
set3=set1.union(set2)
Find the intersection of the following sets:
set1=set([1,2,3])
set2=set([3,4,5])
set3=set1.intersection(set2)
Find the difference of the following sets
set1=set([1,2,3])
set2=set([3,4,5])
set3=set1.difference(set2)
it will print 1,2
Find the symmetric difference of the following sets :
set1=set([1,2,3])
set2=set([3,4,5])
Also what will be the output?
set1.symmetric_difference(set2)
output:
1,2,4,5
What is another way of finding symmetric difference ?
Using what operator?
set1 ^ set2
How to find the subset of the following sets :
set1 = set[(1,2,3,4)]
set2=set[(2,3)]
set2.issubset(set1)
Output : True
What does set1.issubset(set2) return?
False.
How to find the superset of the following sets :
set1 = set[(1,2,3,4)]
set2=set[(2,3)]
set1.issuperset(set2)
Another way to find if set2 is a subset of set1?
set2 <= set1
Another way to find if set2 is SUPERSET Of set1?
set2 >= set1
When you execute this : what are the elements stored?
myset = set (‘Jupiter’)
j,u,p,i,t,e,r
not ordered of course
How to store “JUPITER”
myset = set([‘Jupiter’])
What is the difference between discard and remove in set?
remove raises a KeyError Exception whereas discard does not.
What is serializing an object?
It is the process of converting the object to a stream of bytes that can be saved to a file for later retrieval.
What is object serialization in phyton known as?
pickling
Explain the steps to pickle/ serialize an object :
- Open a binary file for writing
- call the pickle model’s method dump for pickling object n write it to a special file
- close the file after pickling.
What is the mode for binary writing?
‘wb’
Syntax for pickling
import pickle dic = {"Peter" : "Spiderman", "Kitty" : "Shadowcat"} outputfile = open ("file.dat", "wb") pickle.dump = (dic, outputfile) outputfile.close()
What is the mode for binary reading?
“rb”
Syntax for unpickling
import pickle read = open ("data.dat". "rb") a = pickle.load(infile) print a read.close()