May 2017
Intermediate to advanced
280 pages
6h 2m
English
In order to access the dictionary's values you will need the key. Consider a dictionary of networking ports: In order to access the dictionary's values you will need the key. Consider a dictionary of networking ports:
Port = {80: “HTTP”, 23 : “Telnet”, 443 : “HTTPS”}
Let's learn by example:
>>> port = {80: "HTTP", 23 : "Telnet", 443 : "HTTPS"}>>> port[80]'HTTP'>>> port[443]'HTTPS'
In order to access the dictionary's value, use the square brackets along with the key. What happens if the key is not in the dictionary?
>>> port[21]Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> port[21]KeyError: 21>>>
If the key is not found, then the interpreter shows the preceding error.
Read now
Unlock full access