Python 2: what you will learn and the big picture
What you will learn. In "Python 1" you stored one value in one variable. Real networks have many devices, so now you will store many values and let the script repeat work and decide. You will learn lists (many values in order), dictionaries (a value looked up by name), for loops (do it for every item), and if / elif / else (do it only when a condition is true). In the labs you build a small device inventory and then loop over three live routers, count their up interfaces and flag one that has a down interface.
Prerequisites. The module "Python 1": variables, strings, f-strings, comparisons and reading an error message. If the word f-string worries you, revisit that module first.
Analogy: a spreadsheet
Think of the inventory spreadsheet your team already keeps. A list is one column: every router IP, in order. A dictionary is one row with headers: name, IP, role, site and the values next to them. A for loop is "go down the rows, one by one". An if is a conditional rule: "if role is core, colour the row red". With only those four ideas you can read almost any structured data a network script meets.
The four ideas of this module: store, label, repeat, decide.
What the finished labs produce
Lab 1 turns a dictionary of four devices (plus one you add) into three small reports. This is the real output of the finished inventory.py:
AUTO> python3 inventory.py All devices: PUN-CORE-01 -> 10.10.0.1 PUN-ACC-01 -> 10.10.0.11 BLR-CORE-01 -> 10.20.0.1 BLR-ACC-01 -> 10.20.0.11 HYD-ACC-01 -> 10.30.0.11 Total: 5 Core devices: PUN-CORE-01 BLR-CORE-01 Per site: {'Pune': 2, 'Bengaluru': 2, 'Hyderabad': 1}
Lab 2 logs in to three routers, reads the interface table of each, counts the up interfaces and warns about any interface that is down (but not about a port that was shut on purpose):
AUTO> python3 check_up.py
MUM-R1: 2 interfaces up
MUM-R2: 3 interfaces up
MUM-R3: 2 interfaces up
WARNING MUM-R3: down -> ['GigabitEthernet0/2']
Check finished
By the end of the module you will have written the lines that make this output appear.
The nine chapters
- Big picture (this chapter)
- Lists
- Dictionaries
- Nested data: an inventory
- for loops and range()
- Decisions: if / elif / else
- Counting and collecting (Lab 1)
- Loop over routers (Lab 2)
- Summary and checklist
Common mistake. Skipping straight to the labs. The labs ask you to edit specific line numbers with exact indentation; a quick read of chapters 2 to 6 saves a lot of frustration.
Exam trap. The 350-901 exam is full of lists and dictionaries, because API answers (JSON) are exactly nested lists and dictionaries. If you can read a loop over a dictionary, you can read most API-handling code on the exam.
The spreadsheet that became code
A team kept 300 devices in a spreadsheet and copied rows into scripts by hand. They moved the same columns into a Python dictionary, one entry per device. Now one loop can answer "which core devices are in Pune?" and the data is checked into version control instead of living on one laptop.
Lesson: data in lists and dictionaries is something a program can search and count.
"Why are lists and dictionaries so important for network automation?"
Device inventories, parsed show output and API responses (JSON) are all made of nested lists and dictionaries. Lists hold ordered collections such as device IPs; dictionaries hold labelled fields such as an interface's IP and status. Loops and conditions then process them.
Key takeaways
- A list is many values in order; a dictionary looks a value up by its key.
- A for loop repeats work for every item; if / elif / else chooses what to do.
- Labs: build an inventory, then loop over three routers and flag a down interface.
- These shapes are exactly what API data (JSON) looks like.