Starting out With Python Chapter 9

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

This operator can be used to find the intersection of two sets.

&

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

'rb'

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

'wb'

This operator can be used to find the difference of two sets.

-

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

What is the number of the first index in a dictionary?

Dictionaries are not indexed by number.

A list can be a dictionary key.

False

A set comprehension is written just like a list comprehension, except that a set comprehension is enclosed in angled brackets (<>), and a list comprehension is enclosed in square brackets ([ ]).

False

Sets are created by using curly braces {}.

False

Sets are immutable.

False

The dictionary method popitem does not raise an exception if it is called on an empty dictionary.

False

The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs.

False

The following statement creates an empty set: myset = ()

False

The keys in a dictionary must be mutable objects.

False

You can store duplicate elements in a set.

False

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 dictionary's 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

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

The integer 1472

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

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

The pop method accepts a key as ab argument, returns the value that is associated with that key, and removes that key-value pair from the dictionary. The popitem method returns a randomly selected key-value pair, as a tuple, and 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(25)

The set will contain one element: 25.

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('www xxx yyy zzz')

The set will contain these elements (in no particular order): 'w', ' ', 'x', 'y', and 'z'.

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

The set will contain these elements (in no particular order): 'www', 'xxx', 'yyy', and 'zzz'.

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.

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

The set will contain these elements (in no particular order): 10, 9, 8, 'a', 'b', and 'c'.

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

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

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

The string 'start'

A dictionary can include the same value several times but cannot include the same key several times.

True

A tuple can be a dictionary key.

True

Dictionaries are not sequences.

True

If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised.

True

Sets store their elements in an unordered fashion.

True

The difference of set1 and set2 is a set that contains only the elements that appear in set1 but do NOT appear in set2.

True

The following statement creates an empty dictionary: mydct = {}

True

The issubset() method can be used to determine whether set1 is a subset of set2.

True

The remove method raises an exception if the specified element is not found in the set.

True

The set remove and discard methods behave differently only when a specified item is not found in the set.

True

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 do 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.

This operator can be used to find the symmetric difference of two sets.

^

You can add one element to a set with this method.

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.

discard

The _________ dictionary method returns the value associated with a specified key. If the key is not found, it returns a default value.

get()

In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the _________ operator.

in

You can use _________ operator to determine whether a key exists in a dictionary.

in

The _________ method returns all of a dictionary's keys and their associated values as a sequence of tuples.

items()

What would you use to get the number of elements in a dictionary?

len

The _________ function returns the number of elements in a dictionary.

len()

The following function returns the number of elements in a set:

len()

What function do you call to pickle an object?

pickle.dump

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

pickle.load

Which method would you use to get the value associated with a specific key and remove that key-value pair from the dictionary?

pop

The _________ method returns the value associated with a specified key and removes that key-value pair from the dictionary.

pop()

The _________ method returns a randomly selected key-value pair from a dictionary.

popitem()

This set method removes an element and raises an exception if the element is not found.

remove

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

set1 is a superset of set2

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?

set2 is a subset of set1

You use _________ to delete an element from a dictionary.

the del statement

You can add a group of elements to a set with this method.

update

What will be displayed after the following code executes? cities = {'GA': 'Atlanta', 'NY': 'Albany', 'CA': 'San Diego'} if 'FL' in cities: del cities ['FL'] cities ['FL'] = 'Tallahassee' print (cities)

{'CA': 'San Diego', 'NY': 'Albany', 'GA': 'Atlanta'}

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)

{'a', 'd'}

What values will d2 contain after the following code executes? d = {1 : 10, 2 : 20, 3 : 30} d2 = {k : v for k, v in d.items ()}

{1 : 10, 2 : 20, 3 : 30}

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

{1, 2}

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

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

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

{3, 4}

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

{5, 6}

You can use _________ to create an empty dictionary.

{}

This operator can be used to find the union of two sets.

|


संबंधित स्टडी सेट्स

Domain 1- Information Security Governance questions

View Set

Nursing Care of the Child With an Alteration in Cellular Regulation/Hematologic or Neoplastic Disorder

View Set

ch 2-10 critical reading guide quiz

View Set

SCM 14: Supply Chain Risk and Security

View Set

Anatomy Endocrine System Ch. 10 Test

View Set