Dictionary and Sets Flashcards

(55 cards)

1
Q

What is a dictionary?

A

An object that stores a collection of data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A dictionary has two parts. What are they?

A

key

value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you locate a specific value

A

using key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Webster analogy

A
words = keys
definition = values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Key-Value pairs are often referred as?

A

Mappings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why is key-value pairs referred as mappings?

A

Because each key is mapped to a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create a simple dictionary and explain..

A

lastname = {‘Ryan’: ‘Higa’, ‘Hanna’ : ‘Martin’, ‘Justin : Timber’}
»> lastname
»> lastname [‘Ryan’] –ryan = key
then it gives u value lastname : Higa.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Keys must be what?

A

immutable objects. cannot be lists

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Syntax for adding new elements

A

phonebook[‘Sakura’] = ‘Tokyo, Japan’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can you have duplicate keys in the dictionary?

A

Nope. Even if you do it will just replace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Syntax for deleting?

A

del phonebook[“Sakura”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Syntax for checking if sakura is in the dictionary

A

if ‘Sakura’ not in phonebook
print(“Sorry not in the set”)
if ‘Sakura’ in phonebook
print(“In the phonebook”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you get the number of elements in a dictionary?

A

using len function.

Eg. len(phonebook)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you create an empty dictionary?

A

dictionaryname = {} or dict()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

syntax for using loop and printing something in a dictionary

A

for key in phonebook:
print(key) #for key
print(phonebook[key]) # for value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the clear method do?

A

It clears all the elements of the dictionary

phonebook.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does the get method do?

A

gets a value from the dictionary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Will it raise an exception is key is not found?

A

No, the method returns default.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How to print everything in dictionary

A

phonebook.items()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How to print only the keys

A

phonebook.keys()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

how to print only the values

A

phonebook.values()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is a set?

A

A set is a collection of unique values and works like a mathematical set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Name the rules of set.

A

All the elements in the set must be unique
they are unordered.
the elements can be of different data types.

24
Q

How do you create a set?

A

must use a built in function

myset = set()

25
How do you create an empty set?
myset = set()
26
How do you create a set with a,b,c?
myset = set(['a','b','c'])
27
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
28
How to get the number of elements in the set?
len(myset)
29
How to add 1 and 2 to myset?
myset. add(1) | myset. add(2)
30
How to add 4,5,6,7,8,9 to myset?
myset.update([4,5,6,7,8,9)]
31
How to remove a, b and c from myset?
myset. remove('a') myset. remove('b') myset. remove('c')
32
How to remove all the items from myset?
myset.clear()
33
How to print all the elements from myset using loops (for loop)?
for count in myset: | print (count)
34
newset = set([1,2,3]) | Check if 1 is in the newset and print if found
if 1 in newset: | print("Found")
35
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")
36
Find the union of the following sets: set1=set([1,2,3]) set2=set([3,4,5])
set3=set1.union(set2)
37
Find the intersection of the following sets: set1=set([1,2,3]) set2=set([3,4,5])
set3=set1.intersection(set2)
38
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
39
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
40
What is another way of finding symmetric difference ? | Using what operator?
^ | set1 ^ set2
41
How to find the subset of the following sets : set1 = set[(1,2,3,4)] set2=set[(2,3)]
set2.issubset(set1) | Output : True
42
What does set1.issubset(set2) return?
False.
43
How to find the superset of the following sets : set1 = set[(1,2,3,4)] set2=set[(2,3)]
set1.issuperset(set2)
44
Another way to find if set2 is a subset of set1?
set2 <= set1
45
Another way to find if set2 is SUPERSET Of set1?
set2 >= set1
46
When you execute this : what are the elements stored? | myset = set ('Jupiter')
j,u,p,i,t,e,r | not ordered of course
47
How to store "JUPITER"
myset = set(['Jupiter'])
48
What is the difference between discard and remove in set?
remove raises a KeyError Exception whereas discard does not.
49
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.
50
What is object serialization in phyton known as?
pickling
51
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.
52
What is the mode for binary writing?
'wb'
53
Syntax for pickling
``` import pickle dic = {"Peter" : "Spiderman", "Kitty" : "Shadowcat"} outputfile = open ("file.dat", "wb") pickle.dump = (dic, outputfile) outputfile.close() ```
54
What is the mode for binary reading?
"rb"
55
Syntax for unpickling
``` import pickle read = open ("data.dat". "rb") a = pickle.load(infile) print a read.close() ```