⚡ IT Wisdom The intern asked what VLAN stands for. I said "Very Large Anxiety Nightmare." He believed me.
Skilled Practitioner
4249 XP 1751 to Expert
Units
Whetting Your Appetite — Why Python for Networking? Using the Python Interpreter An Informal Introduction to Python — Network Basics Control Flow — Making Decisions in Network Scripts Data Structures — Modeling Network Inventories Modules — Organizing Your Network Scripts Input and Output — Reading and Writing Network Data Errors and Exceptions — Building Reliable Network Scripts Classes — Modeling Network Devices as Objects Standard Library Part I — Tools Every Network Script Needs Standard Library Part II — Logging, Formatting, and Counting Virtual Environments and Packages — Isolating Your Dependencies Capstone — Build a Network Inventory Tool

Numbers in Networking

Python handles numbers exactly as you would expect. Integers are whole numbers — VLAN IDs, port numbers, hop counts. Floats are decimals — bandwidth in Gbps, latency in milliseconds.

vlan_id      = 10
ssh_port     = 22
hop_count    = 4
bandwidth    = 1.25

Python does the math too. ** means "to the power of":

>>> 2 ** 8
256
>>> 2 ** 8 - 2
254

A /24 has 256 addresses. Subtract network and broadcast and you have 254 usable hosts. One line of Python, no calculator needed.


Strings — Hostnames, IPs, Interface Names

A string is any text inside quotes. In networking, almost everything starts as a string — hostnames, IP addresses, interface names, device types.

hostname   = "sw-core-01"
ip_address = "192.168.10.1"
interface  = "GigabitEthernet0/0/1"
vendor     = "Cisco"

Strings remember their content and their length:

>>> hostname = "sw-core-01"
>>> len(hostname)
10
>>> type(hostname)
<class 'str'>

String Operations

split() — break a string into parts. IP addresses split naturally on .:

>>> ip = "192.168.10.1"
>>> ip.split(".")
['192', '168', '10', '1']
>>> ip.split(".")[0]
'192'

Slicing — grab a portion of a string using [start:end]:

>>> interface = "GigabitEthernet0/0/1"
>>> interface[:2]
'Gi'
>>> interface[15:]
'0/0/1'
>>> interface[:2] + interface[15:]
'Gi0/0/1'

"GigabitEthernet" is exactly 15 characters. Everything after position 15 is the port number. Slicing gives you Gi + 0/0/1 = Gi0/0/1 — the short interface name.

upper() and lower() — normalize hostnames for comparison:

>>> "SW-CORE-01".lower()
'sw-core-01'
>>> "sw-access-01".upper()
'SW-ACCESS-01'

Lists — Your First Collection

A list holds multiple values in order. Square brackets, values separated by commas:

devices = ["sw-core-01", "sw-access-01", "sw-access-02", "rtr-edge-01"]

Access individual items by index — index starts at 0:

>>> devices[0]
'sw-core-01'
>>> devices[-1]
'rtr-edge-01'

-1 means the last item. -2 means second to last.

Slicing works on lists too:

>>> devices[1:3]
['sw-access-01', 'sw-access-02']

Count items with len():

>>> len(devices)
4

Putting It Together

Numbers, strings, and lists combine naturally:

vlans   = [10, 20, 30, 40]
devices = ["sw-core-01", "sw-access-01"]

print(f"Managing {len(vlans)} VLANs across {len(devices)} devices")

Output:

Managing 4 VLANs across 2 devices

Summary

  • Integers: VLAN IDs, port numbers, hop counts — no quotes needed
  • Strings: hostnames, IPs, interface names — always in quotes
  • split(".") breaks an IP into a list of octets
  • Slicing [start:end] extracts a portion of a string or list
  • Lists store multiple values — access by index, slice, measure with len()
  • ** is "to the power of" — useful for subnet math