Storing Data with Variables

Storing Data with Variables
Table of Contents

This post is part of an ongoing series - see all the posts below.

Python for Network Engineers

Now that you have Python installed and have run your first script, it is time to start learning the actual language. In this chapter, we will cover the building blocks that every Python script is made of - variables and data types.

We will start with variables, look at the most common data types you will use, and then dig deeper into strings, numbers, and booleans. Along the way, we will also cover useful things like f-strings, type conversion, comparison operators, and user input.

Just as a quick reminder from the previous part, the code you see in these examples is what you would type into a Python file. You can call the file anything you like, main.py, test.py, or whatever makes sense to you, as long as it ends in .py. Save the code inside it, then run it from your terminal with python3 main.py. Every code example in this series is meant to be run this way, so feel free to try each one as we go along.

Variables

A variable is a named reference to a value stored in your computer's memory. In simpler terms, it is a name you give to a piece of data so you can use it later in your script. Think of it like a labelled box where you can put something and then refer to it later by the label.

In Python, you create a variable by writing the name, then an equals sign, then the value you want to store.

device_name = "access_01"

Here, device_name is the variable name, = is the assignment operator, and "access_01" is the value we are storing. Later in the script, whenever we use device_name, Python will use the value stored inside it.

To try this out, create a new file in your IDE and save it with a .py extension. For example, call it main.py. Inside that file, add the two lines below and save it.

device_name = "access_01"
print(device_name)

Then open a terminal in the same folder as your file and run it with python3 main.py. You should see the following output.

access_01

Please note that the variable name itself does not need quotes, only the value does (if the value is a string). Quotes tell Python that the value is a string, which is just text. You can use either double quotes or single quotes in Python, and both will work the same way.

Variables can store different types of data such as numbers, text, lists, dictionaries, or even more complex things. We will look at each of these properly in the upcoming sections.

Why Use Variables

You might be wondering why we bother with variables at all. Why not just write the value directly wherever you need it? The answer is reusability. If you store a value in a variable, you can use it as many times as you want, and if it ever needs to change, you only change it in one place.

device_name = "access_01"

print(f"Connecting to {device_name}")
print(f"Backing up config of {device_name}")
print(f"Reloading {device_name}")
Do not worry about the f and the curly braces you see in the print lines, that is called an f-string, and we will cover it very shortly.
Connecting to access_01
Backing up config of access_01
Reloading access_01

If you wanted to change the device name, you only edit one line at the top of the script, and everything below it updates automatically. This becomes incredibly important once your scripts grow beyond a few lines.

Naming Conventions

You can name your variables almost anything, but there are some rules and best practices.

  • Start with a letter or underscore - You cannot start a variable name with a number. 1st_switch will give you an error, but switch_1 is perfectly fine.
  • No spaces - Variable names must be a single continuous string.
  • Case sensitive - Device and device are two completely different things.

In the Python world, the convention for naming variables is called snake_case. This means we keep everything lowercase and use underscores to separate words. It is not a rule that will break your code if you ignore it, but it is the standard used by the community.

  • Good - interface_name, device_ip, packet_loss
  • Bad - InterfaceName, deviceIp, PacketLoss

A well-named variable also makes your code easier to read. x = "192.168.1.1" is technically fine, but device_ip = "192.168.1.1" tells you exactly what is going on at a glance. Get into the habit of giving your variables meaningful names.

Python uses different naming conventions for other things like classes and constants, but we will cover those when they come up.

Reassigning Variables

Python is dynamically typed. This is a fancy way of saying that a variable is not permanently tied to one specific type of data. In some languages, if you say a variable is an integer, it must always be an integer. In Python, variables are flexible.

x = 100 
print(x)
 
x = "Now I am a string" 
print(x)
100
Now I am a string

Here, x starts as an integer (100). We then reassign x to be a string. While this is possible, try to avoid changing data types within the same variable name in your production scripts, as it can make your code confusing to read later.

Python Data Types

In Python, every value has a data type. A data type defines the kind of value you are working with and determines what you can do with it. For example, you can add two numbers together, but you cannot directly add a number to a piece of text without converting it first. Some of the most common data types you will use are listed below.

  • String (str) - for text values
  • Integer (int) - for whole numbers
  • Float (float) - for numbers with decimals
  • Boolean (bool) - for values that are either True or False
  • List (list) - for an ordered collection of items
  • Dictionary (dict) - for key-value pairs of data

Python automatically assigns the data type based on the value you give to a variable. You do not have to declare it. If you put text in quotes, Python knows it is a string. If you put a whole number, Python knows it is an integer. If you put a number with a decimal, Python knows it is a float.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Packetswitch.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.