To store the name and the address of a switch, you can use two variables:
switch_name = "SW1" switch_ip = "10.0.0.2"A dictionary holds them in one variable and gives each value a name called a key:

Figure 1 - The Parts of a Dictionary
You print a dictionary like a list:
switch = { "name": "SW1", "ip": "10.0.0.2" } print(switch) # Output: {'name': 'SW1', 'ip': '10.0.0.2'}In the lab, create a dictionary named
routerwith the keynamethat holds"R1"and the keyipthat holds"10.0.0.1".
Print it and click Run to get the flag.Answer the question below
Enter the flag you obtained.
To read one value, you write its key between brackets after the name of the dictionary:
![In the dictionary switch = {"name": "SW1", "ip": "10.0.0.2"}, switch["ip"] gives the value of the key ip, "10.0.0.2".](https://dphfzu3i54bzu.cloudfront.net/media/10319/python-dictionary-read-key.png)
Figure 2 - A Key Gives Its Value
With
print(), you show this value on the screen:switch = { "name": "SW1", "ip": "10.0.0.2" } print(switch["name"]) # Output: SW1In the lab, print the address of the switch to get the flag.
Answer the question below
Enter the flag you obtained.
To change a value, you write its key between brackets, then
=and the new value:![The line switch["ip"] = "10.0.0.9" changes the value of the key ip from "10.0.0.2" to "10.0.0.9".](https://dphfzu3i54bzu.cloudfront.net/media/10320/python-dictionary-update.png)
Figure 3 - A New Value for a Key
If the key does not exist yet, the same line adds it to the dictionary:
switch = { "name": "SW1", "ip": "10.0.0.2" } switch["ip"] = "10.0.0.9" switch["vlan"] = 20 print(switch) # Output: {'name': 'SW1', 'ip': '10.0.0.9', 'vlan': 20}In the lab, SW2 has a new address.
Change itsipto"10.0.0.30"and add the keymodelthat holds"C9300"to get the flag.Answer the question below
Enter the flag you obtained.
To read every key with its value, you use a loop with
.items().
At each round, Python gives you a key and its value:
Figure 4 - items() Gives Each Key and Its Value
With a comma in
print(), Python prints the two values with a space between them.In the lab, print each key of the dictionary with its value, for example
name: SW1.
Click Run to get the flag.Answer the question below
Enter the flag you obtained.