Python Variables – Right, Let’s Get This Working
Okay so we’re at python variables now. So we’ve got the basics working haven’t we, Python’s installed, you can write scripts, print works fine, comments are sorted, you’ve figured out the indentation business.
Python for Network Engineers Blog 6: Python Indentation
Right so let’s have a look at variables. Now this is where most courses completely lose the plot. They’ll have you storing someone’s name and age, then they’ll get you doing mortgage calculations or something.
I’m sitting there thinking, that’s lovely mate but I’ve got fifty switches to configure. How’s knowing Dave’s favourite colour going to help me automate VLANs then? It’s not is it.
So what we’ll do, we’ll learn variables using actual network stuff. Device hostnames, management IPs, VLAN configs. The stuff you actually work with every day.
So What Do Variables Actually Do Then
Right so a variable is basically just sticking a label on information so you can use it again later. That’s it really.
So think about when you’re configuring devices. You don’t keep typing out “192.168.100.11” every time you need that management IP do you. You’ve probably got it written down somewhere. Maybe in notepad, maybe on a sticky note stuck to your monitor.
Python variables work exactly the same way except Python remembers it for you.
So instead of typing the IP everywhere you do this once:
sw1_mgmt_ip = "192.168.100.11"
Then whenever you need that IP you just use sw1_mgmt_ip and Python knows what you mean. Much cleaner isn’t it.
And here’s the thing, if that IP changes you update it in one place and it changes everywhere. That’s why variables are brilliant.
Why This Actually Matters
So when you’re working on networks you’re constantly dealing with the same information over and over. Device names, IP addresses, VLAN numbers, interface descriptions, all that stuff.
Let’s say you’re writing a script to backup configs from multiple devices. Without variables you end up typing the same hostnames and IPs all over the place.
What happens when one of those hostnames changes? You’ve got to hunt through your entire script and change every single line. Complete pain in the backside.
With variables you define everything once at the top:
switch_name = "SW1-ACCESS"
switch_ip = "192.168.100.11"
Then you use the variable names everywhere else. When something changes you update it once and it changes everywhere the variable gets used. See what I mean?
Now here’s the bit that trips people up. The assignment uses the equals sign but the variable name goes on the left, the value goes on the right:
hostname = "SW1-ACCESS"
Don’t get that backwards or Python will have a proper meltdown.

Variables vs Literals
Right so there’s a difference between the actual value and what you call it. This trips people up sometimes.
A literal is the real thing. “SW1-ACCESS” or 192.168.100.11. The actual text or number.
A variable is what you call it. hostname or mgmt_ip. The name you give to store that information.
Why does this matter? Because most of the time you’ll work with variables instead of literals. Makes your code flexible instead of hardcoded rubbish.
Python’s Naming Rules
Right so Python has some strict rules about what you can call variables. These aren’t suggestions, they’re actual rules. Break them and your code won’t run.
Got to start with a letter or underscore:
hostname = "SW1-ACCESS" # Good
1st_switch = "SW1-ACCESS" # Bad, starts with number
Only letters, numbers, and underscores:
device_ip = "192.168.100.11" # Good
device-ip = "192.168.100.11" # Bad, has hyphen
Python cares about capital letters:
hostname = "SW1-ACCESS" # These are three
Hostname = "R1-EDGE-RTR" # completely different
HOSTNAME = "SW2-ACCESS" # variables

And you can’t use Python’s special words like if, for, class, import. Python uses those for its own stuff.
How to Name Things Properly
Beyond those hard rules there’s ways of naming things that make your code look professional.
Use lowercase with underscores:
device_hostname = "SW1-ACCESS"
management_ip = "192.168.100.11"
Make the names actually mean something:
router_hostname = "R1-EDGE-RTR" # Clear what this is
switch_mgmt_ip = "192.168.100.11" # Obvious
Don’t use daft abbreviations:
rh = "R1-EDGE-RTR" # What's rh supposed to be?
Six months from now you’ll thank yourself for using proper names.
Use networking terms you already know:
interface_name = "GigabitEthernet0/1"
default_gateway = "192.168.100.1"
routing_protocol = "OSPF"
Different Types of Information
So Python automatically figures out what type of information you’re storing. You don’t need to tell it “this is text” or “this is a number”, it works it out from how you write it.
Strings are for text. Put quotes around it:
hostname = "SW1-ACCESS"
location = "Building A"
Integers are whole numbers. No quotes, no decimal points:
vlan_id = 10
port_count = 24
Floats are decimal numbers:
cpu_usage = 23.7
temperature = 45.2
Booleans are True or False. Note the capital letters:
interface_up = True
device_reachable = False

Let’s Look at Some Proper Examples
Right so instead of the usual student database nonsense let’s look at actual network stuff.
Device information:
edge_router = "R1-EDGE-RTR"
edge_router_ip = "192.168.100.1"
core_router = "R2-CORE-RTR"
core_router_ip = "192.168.100.2"
VLAN setup:
sales_vlan = 10
engineering_vlan = 20
guest_vlan = 30
sales_desc = "Sales Department"
Interface monitoring:
uplink_port = "GigabitEthernet0/1"
uplink_status = True
user_port = "GigabitEthernet0/2"
user_status = False
This is the kind of stuff you work with every day.
Changing What’s Stored
You can change what’s in a variable by assigning something new to it:
device_status = "offline"
print(device_status) # shows offline
device_status = "online" # change it
print(device_status) # now shows online
Each time you assign a new value Python forgets the old one. Useful for tracking changing network states.
Mistakes People Make
Let me tell you about the mistakes I see when people start learning this.
Forgetting quotes around text:
hostname = SW1-ACCESS # Error, Python thinks SW1-ACCESS is a variable
hostname = "SW1-ACCESS" # Right, quotes make it text
Trying to use hyphens:
R1-EDGE-RTR = "192.168.100.1" # Error, hyphen not allowed
R1_EDGE_RTR = "192.168.100.1" # Good, underscore works
Mixing naming styles:
deviceName = "SW1-ACCESS" # Mixing different
device_ip = "192.168.100.11" # styles looks
DeviceType = "switch" # messy
Pick one style and stick with it.

What’s Coming Next
So that’s variables sorted. You understand what they are, how to create them, Python’s naming rules, the basic types you can store.
Next we’ll look at working with text properly. Python strings. How to manipulate device names, parse show command output, handle config templates. All the text stuff you deal with constantly.
For now practice creating variables with actual network information. Device hostnames, management IPs, VLAN numbers. Get comfortable storing network data with proper variable names.
Variables are the foundation for everything else. Get this right and the rest becomes much easier.
External Link: PEP 8 Naming Conventions – official Python style guide for variable naming standards
.

Pingback: Python for Network Engineers Blog 8: Python Strings - RichardKilleen