Let me start by saying that I'm a Network Engineer who has been using Python for several years, and I firmly believe that every network engineer should learn to code. The best and easiest language to start with is Python. I give you my word (just kidding), if you dedicate just 1 hour a day for 2 to 3 weeks, you'll be able to start writing Python code. Of course, you won't become an expert overnight, but you'll have a solid foundation to build on and improve over time.
Knowing Python will definitely help you progress in your career. I learned Python by reading books because I’m more of a book person. If you're interested, I recommend a book called Automate the Boring Stuff with Python.
That said, this series is for those who don’t want to spend time reading a book and prefer short, to-the-point content instead. Understanding the basics well is the most important part of this journey.
But why should a network engineer learn Python?
Believe it or not, I used to ask myself the same question. I am the type of person who struggles to see the value of a tool unless I can clearly see a use case. If someone says, “Hey, you should learn this tool,” I will not bother unless I can relate it to something practical or see it in action. Python was no different for me. So let me give you a few use cases that completely changed my mind.
- If you manage hundreds of devices and you do not have a backup solution, with around 30 to 50 lines of Python code, you can back up all your devices.
- If you need to push some configuration changes to a large number of devices, Python makes this easy.
- If you want to dynamically generate configurations for many devices on the fly, Python can do that too.
These are just a few basic examples, but the list goes on. Think about a task you do repeatedly and are tired of doing manually. I can almost guarantee that Python can automate it for you.
A Quick Intro
I’m pretty sure most people who start learning Python for network automation begin with Netmiko. Netmiko is a Python library specifically designed to work well with network devices. The most basic thing you can do with Netmiko is connect to a device, run a command, and get an output.
from netmiko import ConnectHandler
cisco_01 = {
"device_type": "cisco_ios",
"host": "192.168.100.21",
"username": "admin",
"password": "admin"
}
connection = ConnectHandler(**cisco_01)
output = connection.send_command('show interface status')
print(output)
print('Closing Connection')
connection.disconnect()
It looks very simple, but if you just copy and paste the code and try to run it (assuming you have a Cisco device on that IP), it may not work for you, especially if you are completely new to Python. Even though this is only a few lines of code, there are many things going on.
- What is
from
andimport
? - Do you already have Netmiko installed, or do you need to install it?
- What does the double asterisk
**
mean? - Why are curly braces used?
- Why do some words have quotes and others do not?
Knowing these basics is important. If you do not understand the minimum basics, you will run into problems later.
Learning Roadmap
We'll explore the basics of programming in Python, including variables, data types, operators, loops, and functions. These are the building blocks of any Python program, and understanding them is very important.
- Installing Python on your machine
- Running your first "Hello World!" program
- Variables - Understanding and using variables
- Data Types:
- Strings
- Integers
- Floats
- Lists
- Tuples
- Dictionaries
- Booleans
- Expressions and Operators:
- Arithmetic operators
- Comparison operators
- Logical operators
- Loops:
- For loops
- While loops
- Functions - Defining and calling functions
- Importing Modules
- PIP
- Netmiko - Using Netmiko for network device management

Learning Python varies from person to person, so there's no strict path to follow. My advice? Take your time and make sure you really understand each part before moving on. Don't skip over things you find tough, as it'll only make future learning harder.
Installing Python
Most Linux distributions already come with Python pre-installed. If you're using Linux, you likely have both Python 2 (command: python
) and Python 3 (command: python3
) available. The main difference is that Python 2 is legacy and no longer supported since 2020, while Python 3 is the current version with ongoing updates and improvements. I recommend using Python 3 for all new projects.
Before you proceed with the installation, make sure you check whether or not you have Python installed. Run either python --version
or python3 --version
on your machine, and if you get an output with version 3.x
then you are good to go. I would recommend you have at least Python 3.9 or 3.10, but don't worry if your version is lower, because you can still progress. I don't want to complicate things at this stage.
Mac OS also comes with Python pre-installed. You can check by opening Terminal and typing python3 --version
.
python3 --version
Python 3.10.12
For Windows users, Python doesn't come pre-installed. You'll need to download and install it manually. Visit the official Python website for Windows installation instructions: https://www.python.org/downloads/windows/
After installation, I recommend verifying your Python setup by opening a terminal or command prompt and typing the following.
python --version
or
python3 --version
As I said before, when you run this command, make sure you get an output something like python 3.x
If you only see Python 2.x
, please install Python 3.x
before proceeding.
Python vs Python3
On macOS and Linux systems, you might encounter two different terminal commands for running Python - python
and python3
. This distinction exists mainly due to the transition from Python 2 to Python 3, which are incompatible with each other in some significant ways.
python
usually refers to Python 2 on older systems. For a long time, Python 2 was the default version of Python installed on many Linux distributions and macOS.python3
explicitly refers to Python 3, the current and actively developed version of Python. It includes new features and improvements over Python 2, making it the preferred choice for new projects.
The reason for having these two commands is to allow both Python 2 and Python 3 to coexist on the same system without conflict, catering to applications and scripts that rely on a specific version. Over time, as Python 2 has reached the end of its life (EOL in January 2020), python3
is becoming the standard command to invoke Python, and the python
command is increasingly being linked to Python 3 in newer system installations and environments.
python --version
doesn't point to Python 3, try using python3 --version
to locate it. Moving forward, use the command that points to Python 3.x for your work.