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:
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:
07) The len()
function returns the number of pairs contained in the directory. For example, the following line outputs 0
to the screen:
08) Changing a value of the existing key is done by an assignment. For example, the following snippet outputs False
to the screen:
09) Adding a new pair to the dictionary resembles a regular assignment. For example, the following snippet outputs 2
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:
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:
Last updated