Starting Out Python: Chapter 09 Review

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

When you open a file for the purpose of retrieving a pickled object from it, what file access mode do you use?

'rb' To open a file for binary reading, you use 'rb' as the mode when you call the open function. ex. inputfile = open('mydata.dat', 'rb')

When you open a file for the purpose of saving a pickled object to it, what file access mode do you use?

'wb' The wb indicates that the file is opened for writing in binary mode. ex. outputfile = open('mydata.dat', 'wb')

What will the following code display? stuff = {1 : 'aaa', 2 : 'bbb', 3 : 'ccc'} for k in stuff: print(k)

1 2 3

What will the following code display? stuff = {1 : 'aaa', 2 : 'bbb', 3 : 'ccc'} print(len(stuff))

3

This operator can be used to find the union of two sets. a. I b. & c. - d. ^

I

What is the difference between the remove and discard methods?

If the specified element to delete is not in the set, the remove method raises a KeyError exception, but the discard method does not raise an exception

Suppose a dictionary named inventory exists. What does the following statement do? del inventory[654]

It deletes the element that has the key 654.

What does the items method return?

It returns all a dictionarys keys and their associated values as a sequence of tuples

What does the keys method return?

It returns all the keys in a dictionary as a sequence of tuples.

What does the values method return?

It returns all the values in the dictionary as a sequence of tuples

Suppose a dictionary named employee has been created. What does the following statement do? employee['id'] = 54321

It stores the key-value pair 'id' : 54321 in the employee dictionary.

An element in a dictionary has two parts. What are they called?

Key and value

Does a set allow you to store duplicate elements?

No

Which part of a dictionary element must be immutable?

The key

What module do you import if you want to pickle objects?

The pickle module Once you have opened a file for binary writing, you call the pickle module's dump function. ex. pickle.dump(object, file)

What is the difference between the dictionary methods pop and popitem?

The pop method: 1) accepts a key as an argument, 2)returns the value that is associated with that key, 3) and removes that key-value pair from the dictionary. The popitem method: 1) returns a randomly selected key-value pair, as a tuple, 2) removes that key-value pair from the dictionary.

What is object serialization?

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

After the following statement executes, what elements will be stored in the myset set? myset = set('Jupiter')

The set will contain these elements (in no particular order): 'J', 'u', 'p', 'i', 't', 'e', and 'r'.

After the following statement executes, what elements will be stored in the myset set? myset = set([1, 2, 2, 3, 4, 4, 4])

The set will contain these elements (in no particular order): 1, 2, 3, and 4.

Suppose 'start' : 1472 is an element in a dictionary. What is the key? What is the value?

The string 'start' is the key, and the integer 1472 is the value.

After the following statement executes, what elements will be stored in the myset set? myset = set(25)

TypeError: 'int' object is not iterable

Are the elements of a set ordered or unordered?

Unordered

How do you create an empty set?

You call the built-in set function.

How can you determine whether a key-value pair exists in a dictionary?

You can use the in operator to test for a specific key.

How can you determine whether a specific element exists in a set?

You can use the in operator to test for the element

How do you determine the number of elements in a set?

You pass the set as an argument to the len function.

You can add one element to a set with this method. a. append b. add c. update d. merge

add

What will the following code display? stuff = {1 : 'aaa', 2 : 'bbb', 3 : 'ccc'} print(stuff[3])

ccc

This set method removes an element but does not raise an exception if the element is not found. a. remove b. discard c. delete d. erase

discard

The ____ dictionary method returns the value associated with a specified key. If the key is not found, it returns a default value. a. pop( ) b. key( ) c. value( ) d. get( )

get( )

You can use the ____ operator to determine whether a key exists in a dictionary. a. & b. in c. ^ d. ?

in

The ____ method returns all of a dictionary's keys and their associated values as a sequence of tuples. a. keys_values( ) b. values( ) c. items ( ) d. get( )

items ( )

The ____ function returns the number of elements in a dictionary: a. size( ) b. len( ) c. elements( ) d. count( )

len( )

The following function returns the number of elements in a set: a. size( ) b. len( ) c. elements( ) d. count( )

len( )

Assume the following list exists: names = ['Chris', 'Katie', 'Joanne', 'Kurt'] Write a statement that uses a dictionary comprehension to create a dictionary in which each element contains a name from the names list as its key, and the length of that name as its value.

names = ['Chris','Katie','Joanne','Kurt'] D = {i:len(i) for i in names} print(D) {'Chris': 5, 'Katie': 5, 'Joanne': 6, 'Kurt': 4}

Assume the following dictionary exists: phonebook = {'Chris':'919-555−1111', 'Katie':'828-555−2222', 'Joanne':'704-555−3333', 'Kurt':'919-555−3333'} Write a statement that uses a dictionary comprehension to create a second dictionary containing the elements of phonebook that have a value starting with '919'.

phonebook = {'Chris':'919-555-1111', 'Katie':'828-555-2222', 'Joanne':'704-555-3333','Kurt':'919-555-333'} D = {i: phonebook[i] for i in phonebook if phonebook[i][0:3]=='919'} print(D)

What function do you call to pickle an object?

pickle.dump

What function do you call to retrieve and unpickle an object?

pickle.load

The ____ method returns the value associated with a specified key and removes that key-value pair from the dictionary . a. pop( ) b. random( ) c. popitem( ) d. rand_pop( )

pop( )

The ____ method returns a randomly selected key-value pair from a dictionary. a. pop( ) b. random( ) c. popitem( ) d. rand pop( )

popitem( )

This set method removes an element and raises an exception if the element is not found. a. remove b. discard c. delete d. erase

remove

After the following code executes, what elements will be members of set3? set1 = set(['a', 'b', 'c']) set2 = set(['b', 'c', 'd']) set3 = set1.symmetric_difference(set2)

set1 = set(['a', 'b', 'c']) set2 = set(['b', 'c', 'd']) set3 = set1.symmetric_difference(set2) print(set3) {'a', 'd'}

After the following code executes, what elements will be members of set3? set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set1.difference(set2)

set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set1.difference(set2)print(set3) {1, 2}

After the following code executes, what elements will be members of set3? set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set1.intersection(set2)

set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set1.intersection(set2) print(set3) {3, 4}

After the following code executes, what elements will be members of set3? set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set2.difference(set1)

set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set2.difference(set1)print(set3) {5, 6}

Look at the following code: set1 = set([1, 2, 3, 4]) set2 = set([2, 3]) Which of the sets is a subset of the other? Which of the sets is a superset of the other?

set1 [set1 = set([1, 2, 3, 4])] is a superset set2 [ set2 = set([2, 3])] is a subset

You use ____ to delete an element from a dictionary .a. the remove method b. the erase method c. the delete method d. the del statement

the del statement

You can add a group of elements to a set with this method. a. append b. add c. update d. merge

update

You can use ____ to create an empty dictionary. a. { } b. ( ) c. [ ] d. empty( )

{ }

After the following statement executes, what elements will be stored in the myset set? myset = set('www xxx yyy zzz')

{'x', 'z', 'w', ' ', 'y'}

After the following statement executes, what elements will be stored in the myset set? myset = set(['www', 'xxx', 'yyy', 'zzz'])

{'zzz', 'xxx', 'yyy', 'www'}

After the following statement executes, what elements will be stored in the myset set? myset = set([10, 9, 8]) myset.update([1, 2, 3])

{1, 2, 3, 8, 9, 10}

After the following code executes, what elements will be members of set3? set1 = set([10, 20, 30]) set2 = set([100, 200, 300]) set3 = set1.union(set2)

{20, 100, 200, 10, 300, 30}

After the following statement executes, what elements will be stored in the myset set? myset = set([10, 9, 8]) myset.update('abc')

{8, 9, 10, 'a', 'b', 'c'}


Ensembles d'études connexes

Psychology Chapter 7 Study guide

View Set

Nature of law and the English legal system

View Set

Jacob's BIG FAT QUIZ QUESTION BANK (as a quizlet)

View Set

Everythings an Argument Chapter 7-8

View Set

Nutrition chapter 7 sb questions

View Set

Chapter 9 (Assessment of Intelligence)

View Set

AT Exam #2- Hemodynamic Monitoring, Sepsis, Shock, MODS (Chapters 65 & 66)

View Set