Python For Network Engineers - Introduction (I)

Python For Network Engineers - Introduction (I)
In: Python-Intro

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, everything you build on top will be stronger and more reliable. This solid foundation is key to your long-term success.

My aim here is to keep everything very simple and straightforward. I believe in cutting through the complexity and making learning accessible and enjoyable. We'll cover the basics of Python in a way that directly applies to network engineering.

What We Will Learn?

In this series, we'll cover everything you need to get started with Python. We'll begin by showing you how to install Python on your machine. Then, we'll dive into running your first 'Hello World!' program, a traditional way to start your programming journey.

From there, 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 crucial for your success. We'll also introduce you to Netmiko, a powerful Python module specifically designed for managing network devices.

Learning Roadmap

  • 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. Practice is key, write code, try out examples, and apply what you learn to real tasks, especially those related to your work. This hands-on experience is invaluable. And remember, the Python community is huge and supportive, so don't shy away from asking for help or sharing what you've learned.

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 it 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 one is lower because you can still progress. I don't want to over complicate things at this stage. However, if you only have Python 2.x then you must install Python 3.

Mac OS also comes with Python pre-installed, typically Python 2.7 by default. You can check by opening Terminal and typing python --version. For Python 3, which you'll need for modern development, you might need to install it separately. The easiest way is using Homebrew.

brew install python3

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

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.

💡
Please ensure you have the latest version of Python installed on your machine, ideally Python 3.11 or 3.12, to take advantage of the newest features and security improvements. If 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.
Python - Hello World (II)
In this second part, we will cover how to install Python and VS Code and then run a very basic program. Our ultimate goal here is, when we run this Python code
Written by
Suresh Vina
Tech enthusiast sharing Networking, Cloud & Automation insights. Join me in a welcoming space to learn & grow with simplicity and practicality.
Comments
More from Packetswitch
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.