Interpret
6.1 Interpret basic Python components and scripts
Aligned to Cisco's 350-401 ENCOR v1.2 exam topics.
On this page
What this objective tests
This is an interpret objective. The exam gives you a short Python script. You must say what it does or what it prints.
You do not write production code. You read code like a troubleshooting flow: variables, loops, conditionals, functions, and API calls.
The five building blocks
Python network scripts use a small set of pieces. Learn these first.
- Variable: a name that points to a value. Example:
host = "10.10.10.11". - List: an ordered collection. Example:
["Gi1", "Gi2"]. Indexes start at0. - Dictionary: key-value pairs. Example:
{"name": "Gi1", "enabled": True}. API responses become dictionaries. - Loop: repeats work. Example:
for item in items:. - Function: a named block that takes inputs and returns a value. Example:
def get_token():.
Two comparison traps:
=assigns a value.==compares values.print()shows a value on the screen.returnsends a value back from a function.
Example: a RESTCONF interface script
This is the core worked example. The script uses the requests library. It sends a GET request to a RESTCONF device. Then it prints interface names and IPv4 addresses.
import requests
HOST = "10.10.10.11"
USER = "admin"
PASSWORD = "Cisco123"
url = f"https://{HOST}/restconf/data/ietf-interfaces:interfaces"
headers = {"Accept": "application/yang-data+json"}
response = requests.get(
url,
auth=(USER, PASSWORD),
headers=headers,
verify=False
)
if response.status_code == 200:
data = response.json()
interfaces = data["ietf-interfaces:interfaces"]["interface"]
for intf in interfaces:
name = intf["name"]
ipv4 = intf.get("ietf-ip:ipv4", {})
addresses = ipv4.get("address", [])
if addresses:
ip = addresses[0]["ip"]
print(f"{name}: {ip}")
else:
print(f"{name}: no IPv4 address")
else:
print(f"Error: HTTP {response.status_code}")Read the script line by line:
import requestsloads the HTTP library.HOST,USER, andPASSWORDare variables. They hold the device address and credentials.- The f-string builds the URL.
{HOST}is replaced with10.10.10.11. requests.get(...)sends the GET request. Theauthtuple carries the credentials.response.status_codeholds the HTTP result. Theiftests it.response.json()decodes the JSON body into Python dictionaries and lists.data["ietf-interfaces:interfaces"]["interface"]walks the nested structure. The result is a list of interface dictionaries.- The
forloop runs once per interface.intfis the current item. intf.get("ietf-ip:ipv4", {})uses.get()with a default. This avoids a crash when the key is missing.- The inner
ifchecks whether the address list is empty. print(f"{name}: {ip}")shows one line per interface.
Expected output:
GigabitEthernet1: 10.10.10.11
GigabitEthernet2: no IPv4 address
Loopback0: 192.0.2.1Note: verify=False skips certificate validation. Use it only in a lab with self-signed certificates. Production scripts must validate certificates.
How JSON becomes Python
response.json() converts types. Know the mapping:
| JSON | Python |
|---|---|
object {...} | dictionary |
array [...] | list |
string "text" | string |
number 24 | integer or float |
true / false | True / False |
null | None |
After decoding, you walk the structure with keys and indexes. Example: data["ietf-interfaces:interfaces"]["interface"][0]["name"] gives the first interface name.
Lab: read a script
This is a paper lab. No device is necessary. Read the script. Answer the questions. Then check the answers.
import json
def is_problem(intf):
return intf["admin"] == "up" and intf["oper"] == "down"
with open("interfaces.json") as f:
data = json.load(f)
count = 0
for intf in data["interfaces"]:
if is_problem(intf):
count = count + 1
print(intf["name"])
print(f"Total problems: {count}")The file interfaces.json contains:
{
"interfaces": [
{"name": "Gi1/0/1", "admin": "up", "oper": "up"},
{"name": "Gi1/0/2", "admin": "up", "oper": "down"},
{"name": "Gi1/0/3", "admin": "down", "oper": "down"},
{"name": "Gi1/0/4", "admin": "up", "oper": "down"}
]
}Answer these questions:
- What does the function
is_problemreturn forGi1/0/1? (Answer:False. Admin and oper are both up.) - What does
json.load(f)return? (Answer: a Python dictionary.) - How many times does the loop body run? (Answer: four times. The list has four items.)
- What is the value of
countat the end? (Answer:2. Two interfaces are admin up and oper down.) - What does the script print? (Answer: the output below.)
- Why does the script use
intf["name"]and notintf[0]? (Answer:intfis a dictionary. Dictionaries are accessed by key, not by index.)
Expected output:
Gi1/0/2
Gi1/0/4
Total problems: 2Exam traps
- Indexes start at
0.items[0]is the first item. =assigns.==compares.- A loop variable is a temporary name for the current item. It is not special.
.get()with a default prevents a crash on a missing key. Square brackets do not..json()decodes the body. Call it only when a body exists. A204response has no body.- Indentation defines code blocks. Wrong indentation changes the logic.
Pass check
You are ready when you can do these things:
- Track a variable from assignment to final print.
- Predict the output of a loop over a list of dictionaries.
- Explain what
response.status_codeandresponse.json()give you. - Read a function by naming its input, its test, and its return value.
- Spot the difference between JSON
trueand PythonTrue.
Sources used
- Cisco ENCOR 350-401 v1.2 exam topics: https://learningcontent.cisco.com/documents/marketing/exam-topics/350-401-ENCORE-v1.2.pdf