Pcornel
Networking · Automation and Artificial Intelligence

Python Fundamentals

Cheat sheet · https://pcornel.dk/topic.php?topic=90

Back to the Topic

Answer

Python fundamentals for network automation: variables, types (str, int, float, bool, None), expressions, f-strings, imports and reading a script top to bottom.

Remember

Indentation is syntax. Strings from APIs or CLI output are text — convert with int() before arithmetic. Use a virtual environment per project and pin library versions. Read the traceback from the bottom: the last line is the error, the line above it is where.

First checks

  • python3 --version and which python3 — interpreter in use.
  • python3 -m venv .venv && . .venv/bin/activate — isolated environment.
  • pip list — installed libraries (requests, netmiko, ncclient, pyyaml).
  • python3 -m py_compile script.py — syntax check.
  • python3 -i script.py / print(type(x), x) — inspect values while debugging.

Verify in this order

correct interpreter and libraries → script parses → inputs read and validated → logic runs → output or change verified

Where it usually breaks

physical/link dependency → local object state → peer/control state → policy → forwarding/adjacency → end-to-end result

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=90

Confirmation

Confirm action

Continue?