Python for Network Engineers
Conditions & Loops
What you'll learn
- How a for loop repeats an action across every item in a list
- How if / elif / else runs different code depending on a condition
- How to combine a loop and a condition to act on devices selectively
- Why 'one workflow, many devices' is the core mechanic behind network automation
- How to predict what a small loop will print before running it
Listen to a guided explanation of this lesson.
Spoken narration isn't supported in this browser.
One workflow, many devices
Here's the idea this whole lesson is really about, stated plainly before any syntax: network automation is one workflow, applied to many devices. You've seen this idea in every module so far — checking NTP on one switch or forty is the same check, repeated. A for loop is the Python mechanism that does exactly that repeating.
This is the core mechanic
If there's one idea in this whole module worth remembering above the others, it's this: ONE workflow + MANY devices. Almost every automation script is a loop wrapped around logic that would otherwise be typed by hand, once per device.
The for loop
devices = [
{"hostname": "SW1", "ip": "10.10.10.1"},
{"hostname": "SW2", "ip": "10.10.10.2"},
{"hostname": "SW3", "ip": "10.10.10.3"},
]
for device in devices:
print(device["hostname"])'for device in devices' runs the indented line once for every item in the devices list. Each time through, device holds the current item — first {"hostname": "SW1", ...}, then the SW2 dictionary, then SW3. Output: SW1, then SW2, then SW3, one per line.
Notice the loop body — print(device["hostname"]) — is written exactly once, but it runs three times, once per device. That's the entire value of a loop: write the logic once, apply it to every item in the list, no matter how long the list is.
- devicesthe list being looped over
- SW1 → processdevice = the SW1 dictionary
- SW2 → processdevice = the SW2 dictionary
- SW3 → processdevice = the SW3 dictionary
Conditions: if / elif / else
A condition lets a script take different action depending on a fact about the device — this is where a boolean like is_reachable from Lesson 2 actually gets used:
device = {"hostname": "SW1", "reachable": True}
if device["reachable"]:
print("Run automation")
else:
print("Skip device")if device["reachable"]: checks whether that value is True. If it is, the indented line under if runs. If it's False, Python skips straight to else and runs that line instead. Only one branch ever runs.
When there are more than two possible outcomes, elif ("else if") adds additional checks in between:
status = "out_of_sync"
if status == "synced":
print("No action needed")
elif status == "out_of_sync":
print("Flag for review")
else:
print("Unknown status")Python checks each condition in order, top to bottom, and runs the first branch whose condition is True — here, status == "out_of_sync" matches, so "Flag for review" prints, and the rest of the branches are skipped entirely.
Combining a loop and a condition
This is where the two ideas genuinely earn their keep — looping through every device, and only acting on the ones that meet a condition:
devices = [
{"hostname": "SW1", "reachable": True},
{"hostname": "SW2", "reachable": False},
{"hostname": "SW3", "reachable": True},
]
for device in devices:
if device["reachable"]:
print(f"{device['hostname']}: running automation")
else:
print(f"{device['hostname']}: skipped, unreachable")The loop visits every device, one at a time. The condition inside it decides what happens to each one. Output: SW1 runs, SW2 is skipped, SW3 runs — three devices, one piece of logic, applied selectively based on each device's own data.
Not yet: list comprehensions
You may eventually see loops and conditions combined into a single compact line (a 'list comprehension'). That's a shorthand for exactly what you just learned, not a different idea — this module sticks to the spelled-out for/if form, since it's what you'll actually be reading and modifying in real automation code as a beginner.
Key takeaways
- A for loop runs the same block of code once for every item in a list — write the logic once, apply it to every device.
- if / elif / else runs exactly one branch, based on which condition (if any) is True.
- Combining a loop with a condition inside it is how a script visits every device but only acts on the ones that meet some criteria.
- 'ONE workflow + MANY devices' is the core mechanic behind almost every network automation script — this lesson is that mechanic, in actual code.
- Python checks if/elif conditions top to bottom and stops at the first match — later branches don't run once one has matched.