NetAuto Academy

Python for Network Engineers

Variables, Data Types & Basic Operations

Beginner12 min read

What you'll learn

  • How to create and reassign a variable in Python
  • The four basic data types: strings, integers, floats, and booleans
  • Why a value's type matters — and a real gotcha it causes
  • Basic operators: arithmetic, comparison, and string concatenation
  • Why variables let a script work with changing device information instead of hardcoded values
🎓 Teach This Lesson

Listen to a guided explanation of this lesson.

Spoken narration isn't supported in this browser.

Creating a variable

The last lesson showed one variable without explaining the syntax. Here's the full picture: writing name = value creates a variable — a label pointing at a value — that the rest of the script can use by name.

device_facts.py
device_name = "SW1"
device_ip = "10.10.10.1"
ssh_port = 22
is_reachable = True

Four variables, four different kinds of value. device_name and device_ip hold text. ssh_port holds a whole number. is_reachable holds a true/false value. Python doesn't need you to declare the type up front — it figures it out from what you assign.

Each line follows the same pattern: a name on the left, an =, and a value on the right. Nothing runs yet — this just stores four values under four names, the same way you'd note down four facts about a switch before doing anything with them.

Quick checkConcept check
What does the line 'device_ip = "10.10.10.1"' actually do?

The four basic data types

Every value in the example above belongs to one of Python's basic types:

  • String (str) — text, always wrapped in quotes: "SW1", "10.10.10.1". Hostnames, IP addresses, and platform names are almost always strings, even when they look numeric.
  • Integer (int) — a whole number, no quotes: 22, 48. A port number or a VLAN ID is typically an integer.
  • Float — a number with a decimal point: 98.5, 0.75. Less common in day-to-day device facts, but shows up in things like CPU utilization percentages or link latency in milliseconds.
  • Boolean (bool) — exactly True or False, capitalized, no quotes. is_reachable = True is a boolean — a direct yes/no fact about the device.

Why 'string' for text that looks like a number

"10.10.10.1" looks numeric, but it's a string — Python has no built-in concept of an IP address, so it's stored as text and treated as text. This matters more than it sounds like it should, which is exactly what the next section is about.

Why the type actually matters

Here's a mistake almost everyone makes once: comparing a value to the wrong type.

port_check.py
config_port = "22"    # read from a config file as text
ssh_port = 22         # a plain integer

print(config_port == ssh_port)   # False!

This prints False, which surprises people the first time. "22" (a string) and 22 (an integer) look the same to a human, but Python treats them as different values entirely — they're not automatically converted to compare equal.

This isn't a Python quirk to memorize and move on from — it's exactly the kind of bug that shows up in real automation, because config files, command output, and user input often hand you numbers as text. Comparing a port number read from a file against a plain integer without converting one of them first is a genuinely common source of "why did this check silently fail?" moments.

Quick checkConcept check
Why does 'config_port == ssh_port' print False, if config_port is "22" and ssh_port is 22?

Basic operators

A few operators come up constantly in automation scripts:

  • Arithmetic: +, -, *, / — mostly on numbers. vlan_count + 1.
  • Comparison: == (equal), != (not equal), <, > — these produce a boolean (True/False), and are the backbone of every condition you'll write in the next lesson.
  • String concatenation: joining text together. "SW" + "1" produces "SW1". Mixing a string and a number directly with + (like "Port: " + 22) raises an error — you'd need str(22) first, or (the more common, more readable approach) an f-string like f"Port: {22}", which you already saw in Lesson 1.
Quick checkConcept check
Why does 'print("Port: " + 22)' raise an error in Python?

Changing a value

A variable isn't locked in after its first assignment — reassigning it just points the name at a new value:

reassign.py
device_ip = "10.10.10.1"
print(device_ip)   # 10.10.10.1

device_ip = "10.10.10.2"   # same variable, new value
print(device_ip)   # 10.10.10.2

The second assignment doesn't create a second variable — it replaces what device_ip points to. This is exactly what lets one script loop through many devices, updating the same variable to each device's information in turn, instead of needing a differently-named variable for every device.

Quick checkIdentify the correct code
Which line correctly stores the SSH port as an integer (not text) in a variable named ssh_port?

Why this matters for automation

None of this is abstract housekeeping — it's exactly what makes a script able to work with changing device information instead of numbers and names baked directly into the code. A script that hardcodes "10.10.10.1" everywhere it needs a switch's address only ever works for that one switch. A script that reads each switch's IP into a variable — the same device_ip name, filled with a different value each time through a loop — works for one switch or five hundred, without changing a line of logic. That's the whole reason variables matter here: they're what makes "the same script, different devices" possible.

Key takeaways

  • A variable is created with name = value, and can be reassigned to a new value at any time.
  • Python's basic types: string (text), integer (whole numbers), float (decimals), boolean (True/False).
  • Values that look alike but have different types ("22" vs 22) are not equal in Python — this is a real, common source of silent bugs in automation scripts.
  • Comparison operators (==, !=, <, >) produce booleans and are the foundation of every condition you'll write next lesson.
  • Variables are what let a script work with changing device information — the same logic, filled in with a different value each time — instead of hardcoding one device's details.