Question: PYTHON PYTHON PYTHON PYTHON PYTHON PYTHON PYTHON PYTHON PYTHON Question 1: Dictionaries contain key-value pairs. Values are like a list's indexes. True False Question 2:

PYTHON PYTHON PYTHON PYTHON PYTHON PYTHON PYTHON PYTHON PYTHON

Question 1:

Dictionaries contain key-value pairs. Values are like a list's indexes.

True

False

Question 2:

Dictionaries do not consider the key-value pairs to be in any certain order.

True

False

Question 3:

What is the syntax for writing a dictionary in Python?

[ key : value ]

( key, value )

{ value : key }

{ key : value }

Question 4:

When it comes to being able to add, remove, or change values, dictionaries are _____

unchangeable

molten

mutable

immutable

Question 5:

Given this dictionary: user{'name' : 'Bob', 'username' : 'bob42' } What happens when you try to access the dictionary like this: user['age']

A KeyError message is displayed

The user is prompted to enter Bob's age

Bob's age is displayed

A default value is set because there is no 'age' key in the dictionary

Question 6:

What method provides a way to avoid a KeyError message by returning a result when attempting to access keys that may not be present in a dictionary?

items()

values()

has()

get()

Question 7:

What method provides a way to set a default key-value pair when wanting to access a key that may not be present in a dictionary?

items()

default()

has()

setdefault()

Question 8:

What expression would give the same result as the following? if 'age' not in user: user['age'] = 'unknown'

user.setdefault('age', 'unknown')

user.add('age', 'unknown')

if get('age') is False set to 'unknown'

user = { 'age' : 'unknown' }

Question 9:

What module and function can be used to "pretty print" dictionary values?

pprint.print()

pprint.pprint()

pretty.print()

print.pprint()

Question 10:

What type of data does the method items() return when called on a dictionary?

A list of the key and value

A list of the only the values

A list like dict_keys with the keys

A collection of tuples consisting of key and value pairs

Question 11:

Given this dictionary: user{'name' : 'Bob', 'username' : 'bob42' } What structure will allow us to loop through and access both the keys and values in the dictionary?

for v in user.keys()

for k, v in user.items():

for 'name' in user:

if v in user:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!