1.2.1.4 PCEP-30-02 Practice Test Compendium β Tuples and dictionaries
Dictionaries β continued
04) Accessing a dictionary's value requires the use of its key. For example, the following line outputs 911 to the screen:
print(phone_directory['Emergency'])
05) An attempt to access an element whose key is absent in the dictionary raises the KeyError exception.
06) The in and not in operators can be used to check whether a certain key exists in the dictionary. For example, the following line prints True False to the screen:
10) Removing a pair from a dictionary is done with the del instruction. For example, the following snippet outputs 0 to the screen:
currencies ={'USD':'United States dollar'}del currencies['USD']print(len(currencies))
11) When iterated through by the for loop, the dictionary displays only its keys. For example, the following snippet outputs A B to the screen:
12) The .keys() method returns a list of keys contained in the dictionary. For example, the following snippet outputs A B to the screen:
13) The .values() method returns a list of values contained in the dictionary. For example, the following snippet outputs Alpha Bravo to the screen:
14) The .items() method returns a list of two-element tuples, each filled with key:value pairs. For example, the following snippet outputs ('A', 'Alpha') ('B', 'Bravo') to the screen: