Python for IT Automation

Networking· 3 CUs· D522
Performance assessmentDue May 4, 2027
Lessons0 / 17
Python for IT tasks: variables, operators, data structures

Core syntax for scripts

10 min
NeedCode
Text with valuesf"{host}: {ms:.1f} ms"
Integer / float division7 // 2 is 3; 7 / 2 is 3.5
Remaindern % 2 == 0 tests even
Membership"up" in status; port in (22, 23)
Identity with Noneif result is None:
Chained comparison1 <= port <= 65535
Conditional valuestate = "up" if ok else "down"
Convertint("22"), str(22), float("0.5")
TypeMutableTypical IT use
strNoHostnames, command output
int, floatNoPorts, counts, latency
boolNoReachable or not
listYesOrdered device list
tupleNoFixed pairs: (host, port)
setYesUnique IPs; differences between inventories
dictYesOne device's settings by name

Everything read from input(), a file, a CSV or command output is str. "22" == 22 is False.

f-string
f"..." with {expressions}; {:.1f} formats one decimal.
Mutable
Can change in place (list, dict, set).

Knowledge check

port = "22". port == 22

Valid port test for an int p: