What is a Dictionary?
In Python, a
dictionary (data type
dict) is an
unordered,
indexed, and
mutable collection of data
that contains any number of
key-value pairs (obviously,
the key is unique):
Below is an initial example of a dictionary:
DETAILS
For each key word on the left, we have associated a value (an antonym) on the right.
We use the colon character "
:" to make the association, and to separate pairs,
the comma character "
,". All are written
within curly braces, similar to a data set.
The length of the dictionary is not 6, but of course 3, which is the number of key (pairs) we have entered.
A dictionary
is not ordered with actual indexes, but access is done using the key words from the pairs.
Thus, we write
antonyms['tall'] to get the value '
short'.
If we try to write
antonyms[1], we will get an interpretation error:
Moreover, if a key word is not found in the data structure, an exception/error will be displayed:
So, the index (key) was not found.
Proceed to the next page.