Skip to content
Study CCNP

Interpret

6.1 Interpret basic Python components and scripts

4 min read ENCOR 350-401 v1.2 Updated

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 at 0.
  • 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. return sends 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:

  1. import requests loads the HTTP library.
  2. HOST, USER, and PASSWORD are variables. They hold the device address and credentials.
  3. The f-string builds the URL. {HOST} is replaced with 10.10.10.11.
  4. requests.get(...) sends the GET request. The auth tuple carries the credentials.
  5. response.status_code holds the HTTP result. The if tests it.
  6. response.json() decodes the JSON body into Python dictionaries and lists.
  7. data["ietf-interfaces:interfaces"]["interface"] walks the nested structure. The result is a list of interface dictionaries.
  8. The for loop runs once per interface. intf is the current item.
  9. intf.get("ietf-ip:ipv4", {}) uses .get() with a default. This avoids a crash when the key is missing.
  10. The inner if checks whether the address list is empty.
  11. print(f"{name}: {ip}") shows one line per interface.

Expected output:

GigabitEthernet1: 10.10.10.11
GigabitEthernet2: no IPv4 address
Loopback0: 192.0.2.1

Note: 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:

JSONPython
object {...}dictionary
array [...]list
string "text"string
number 24integer or float
true / falseTrue / False
nullNone

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:

  1. What does the function is_problem return for Gi1/0/1? (Answer: False. Admin and oper are both up.)
  2. What does json.load(f) return? (Answer: a Python dictionary.)
  3. How many times does the loop body run? (Answer: four times. The list has four items.)
  4. What is the value of count at the end? (Answer: 2. Two interfaces are admin up and oper down.)
  5. What does the script print? (Answer: the output below.)
  6. Why does the script use intf["name"] and not intf[0]? (Answer: intf is a dictionary. Dictionaries are accessed by key, not by index.)

Expected output:

Gi1/0/2
Gi1/0/4
Total problems: 2

Exam 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. A 204 response 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_code and response.json() give you.
  • Read a function by naming its input, its test, and its return value.
  • Spot the difference between JSON true and Python True.

Sources used

  • Cisco ENCOR 350-401 v1.2 exam topics: https://learningcontent.cisco.com/documents/marketing/exam-topics/350-401-ENCORE-v1.2.pdf

Related objectives