In the previous lesson, you used EEM to automate tasks directly on a router.
EEM is powerful, but the applet runs inside a single device.When you need to manage a fleet of devices from a central place, you write external scripts using Python.
Your Python script does not type CLI commands like a human. It sends structured requests to an API on the device, and the device replies with structured data in JSON.

Figure 1 – The four pieces behind every script that talks to a router
The Four Architecture Pieces
Four pieces work together every time a script talks to a router:
The script you write: variables, lists, dictionaries, and loops that hold your logic.
The library you import: the Python package doing the heavy lifting, such as
requestsorncclient.The protocol on the wire: RESTCONF or NETCONF travelling between your script and the device.
The device that answers: the Cisco IOS-XE router receiving the request, applying it, and replying.
This lesson covers the first piece: the plain Python logic you write to handle network data structures.
Answer the question below
What does the Python script send to a device that exposes an API? (one word, plural)
Answer the question below
In what format does the device reply to a Python script?
Every exam exhibit you read is built from these same building blocks. Master them to read any script instantly.
hostname = "R1" ip_address = "192.168.1.1" port = 830 enabled = TrueThree types cover almost every networking value you will see:
string: text in quotes, like
"R1"integer: a whole number, like
830boolean:
TrueorFalse(capitalized in Python)
Answer the question below
What type is the value assigned to "port" in port = 830?
Lists and Dictionaries
A list stores multiple values in order using square brackets:
interfaces = ["Gi0/0", "Gi0/1", "Gi0/2", "Gi0/3"] print(interfaces[0]) # first element print(interfaces[-1]) # last elementOutput:
Gi0/0 Gi0/3The first element is index 0, not 1.
Negative indexes count backward from the end, making[-1]the final item. This is a classic exam pattern.A dictionary stores values associated with keys using curly braces:
device = { "hostname": "R1", "ip": "192.168.1.1", "port": 830 } print(device["hostname"]) print(device["port"])Output:
R1 830You access a value by its key inside square brackets.
Asking for a key that does not exist raises aKeyError.Answer the question below
In the dict above, what does device["ip"] return?
Answer the question below
With interfaces = ["Gi0/0", "Gi0/1", "Gi0/2", "Gi0/3"], what does print(interfaces[-3]) display?
Network APIs almost never return flat dictionaries.
They return nested structures, which is where most exam exhibits test your reading speed.device = { "hostname": "R1", "ip": "192.168.1.1", "interfaces": [ {"name": "Gi0/0", "status": "up"}, {"name": "Gi0/1", "status": "down"} ] }To reach a deep value, chain the brackets in order.
Each bracket moves you one step deeper into the structure.

Figure 2 – Walk the keys, one bracket at a time
Chaining Brackets and Iteration
Look at this expression:
status = device["interfaces"][0]["status"] print(status)Read it step by step from left to right:
device["interfaces"]pulls the list of interface dictionaries.[0]picks the first dictionary in that list.["status"]extracts the status value inside that dictionary.
Output:
upMany exam questions show you a JSON response and ask for the printed output.
You trace the keys, follow the indexes, and find the value.When the list contains dictionaries, a
forloop lets you process every item in turn:for iface in device["interfaces"]: print(iface["name"], iface["status"])Output:
Gi0/0 up Gi0/1 downOn every iteration,
ifacetakes the value of the current dict in the list.
Inside the loop, you access its keys exactly like a normal dict.You can now read any data structure an exam exhibit puts in front of you: variables, lists, dictionaries, nested access, and loops.
Answer the question below
In the loop above, what is the type of the variable iface on each iteration?
Answer the question below
With the nested device dictionary above, what does print(device["interfaces"][1]["status"]) display?