Dictionary and Sets Flashcards

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
Q

How do you create an empty set?

A

myset = set()

26
Q

How do you create a set with a,b,c?

A

myset = set([‘a’,’b’,’c’])

27
Q

What happens if you create a set like this : myset(‘aabbc’)

A

well the repeating wont be in a set. Instead just this : a,b,c

28
Q

How to get the number of elements in the set?

A

len(myset)

29
Q

How to add 1 and 2 to myset?

A

myset. add(1)

myset. add(2)

30
Q

How to add 4,5,6,7,8,9 to myset?

A

myset.update([4,5,6,7,8,9)]

31
Q

How to remove a, b and c from myset?

A

myset. remove(‘a’)
myset. remove(‘b’)
myset. remove(‘c’)

32
Q

How to remove all the items from myset?

A

myset.clear()

33
Q

How to print all the elements from myset using loops (for loop)?

A

for count in myset:

print (count)

34
Q

newset = set([1,2,3])

Check if 1 is in the newset and print if found

A

if 1 in newset:

print(“Found”)

35
Q

newset = set([1,2,3])

Check if 7 is in the newset and print if not found

A

if 7 not in newset:

print(“Not found”)

36
Q

Find the union of the following sets:
set1=set([1,2,3])
set2=set([3,4,5])

A

set3=set1.union(set2)

37
Q

Find the intersection of the following sets:
set1=set([1,2,3])
set2=set([3,4,5])

A

set3=set1.intersection(set2)

38
Q

Find the difference of the following sets
set1=set([1,2,3])
set2=set([3,4,5])

A

set3=set1.difference(set2)

it will print 1,2

39
Q

Find the symmetric difference of the following sets :
set1=set([1,2,3])
set2=set([3,4,5])
Also what will be the output?

A

set1.symmetric_difference(set2)
output:
1,2,4,5

40
Q

What is another way of finding symmetric difference ?

Using what operator?

A

set1 ^ set2

41
Q

How to find the subset of the following sets :
set1 = set[(1,2,3,4)]
set2=set[(2,3)]

A

set2.issubset(set1)

Output : True

42
Q

What does set1.issubset(set2) return?

A

False.

43
Q

How to find the superset of the following sets :
set1 = set[(1,2,3,4)]
set2=set[(2,3)]

A

set1.issuperset(set2)

44
Q

Another way to find if set2 is a subset of set1?

A

set2 <= set1

45
Q

Another way to find if set2 is SUPERSET Of set1?

A

set2 >= set1

46
Q

When you execute this : what are the elements stored?

myset = set (‘Jupiter’)

A

j,u,p,i,t,e,r

not ordered of course

47
Q

How to store “JUPITER”

A

myset = set([‘Jupiter’])

48
Q

What is the difference between discard and remove in set?

A

remove raises a KeyError Exception whereas discard does not.

49
Q

What is serializing an object?

A

It is the process of converting the object to a stream of bytes that can be saved to a file for later retrieval.

50
Q

What is object serialization in phyton known as?

A

pickling

51
Q

Explain the steps to pickle/ serialize an object :

A
  • 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
Q

What is the mode for binary writing?

A

‘wb’

53
Q

Syntax for pickling

A
import pickle
dic = {"Peter" : "Spiderman", "Kitty" : "Shadowcat"}
outputfile = open ("file.dat", "wb")
pickle.dump = (dic, outputfile)
outputfile.close()
54
Q

What is the mode for binary reading?

A

“rb”

55
Q

Syntax for unpickling

A
import pickle
read = open ("data.dat". "rb")
a = pickle.load(infile)
print a
read.close()