HomeOperationsAutomated OperationsMoving up the stack - time to learn Python - part 1

Moving up the stack – time to learn Python – part 1

Today I am starting on a new journey, many of you will have heard of Python, it is a versatile and powerful programming language that can be used for a variety of tasks in the field of DevOps; it can help in the engineering and automation of workflows, integration of tools, managing data, testing code, monitoring performance, and troubleshooting issues. Python also has a large and active community that provides many libraries, frameworks, and resources for DevOps projects.

Learning Python can enhance the skills and productivity of DevOps engineers and enable them to create high-quality software solutions. This does not mean that I have jumped ship and left GoLang, this is just another arrow in the quiver of useful tools that a fully featured DevOps engineer needs to carry out their role effectively. These are Terraform or another IaC, Ansible or another Config management tool (Puppet, Chef), CICD Tooling, (Azure DevOps, GitHub, GitLab), and extension languages like Go, Ruby, Rust, NodeJS, and Python.

There are many more tools and languages that can improve an engineer, but the above will enable a very rounded development toolset. Now there is no need to be expert in everyone, that is what teams are for, all with different but complementary skill sets to get the job done.

What is Python?

Python is an interpreted, high-level, general-purpose programming language that emphasizes readability and simplicity.

is it a Python or is it Python?
This Python is not a snake, it is a programming language

It has a long legacy being created by Guido van Rossum in the 1990’s as a successor to a language called ABC.  As a language it can be compared to other interpretive languages such as PHPRuby, and JavaScript. All these languages are popular for scripting, rapid prototyping, and creating small to medium-sized applications. Another benefit is that they are apparently easy to learn and use, offer a faster development cycle (we shall see), and are platform-independent.

Why use Python?

There are many reasons why we should learn Python. It is a general-purpose language that can be used to create a variety of different programs. A couple of reasons why we could use Python include the fact that it is easy to read, use and maintain your code, it is versatile and is used for many different task from automation to web-development, it has excellent compatibility with major platforms and systems, it has a large standard library similar to GoLang which makes for a much simpler learning curve as you do not have to re-invent wheels, also these libraries make it a reasonably cost-effective  approach. Additionally, Python has an expansive library of open source data analysis tools, web frameworks, and testing instruments, making its ecosystem one of the largest out of any programming community.

What is the difference between an interpreted and a complied code language?

The most important difference between an interpreted and a compiled code language is how the source code is converted into machine code. In a compiled language, the target machine directly translates the program. The entire program is converted into machine code at once, resulting in a binary program that is optimised for the platform the application is deployed too. This can result in faster and more efficient execution than interpreted languages. In an interpreted language, the source code is not directly translated by the target machine. Instead, a different program, called the interpreter need to be present on the machine to read and execute the code. The source code is converted into machine code line by line. Interpreted languages can be modified while the program is running.

Creating your code environment

Python is a platform agnostic, does that mean that I only need to install in to either Windows MacOS or Linux and my code will work everywhere, Consultant answer “that depends.” A fuller answer is above the scope of this article, but it currently suffices to say that as long as certain guidelines are followed it is agnostic.

Windows

The installation process for Python 3 on Windows is simple, point your browser at the official Python Website and download the latest “winstaller” package, and with only a couple of extra shown below we will have configured our programming environment. These steps will enable us to access Python from anywhere on our system and install extra software using pythons built-in package manager. Finally Installing Python in this manner enables the creation of projects that work with the operating system, for example notifications and automated system tasks.

After downloading the installer Double click on the file and select “Install Launcher for all users”, and ensure that you also select “Add Python 3.10 to PATH” to ensure that the program is added to your machines path.  Click on “Install now” to begin. There is no need to customise the installation the default is to install all options.

Python Installer 1
Python for Windows Installer

After the installation is complete, remember to click “Disable path length limit”, doing so will disable the path length limit meaning you can use more than 260 characters in a file path. Finally click close

Python Installer 2
Windows Installation of Python

And rather oddly Click Close again to end the installation.

Python installer 3
Python installed on Windows.

Alternatively, you can install Python from the Windows Store, this option will only allow access to stable releases, but to be fair pioneers are the ones with the arrows in their hat.  To start this process, simply enter Store in the taskbar search box and open the “Microsoft Store” app

Microsoft Store
You can aslo install python from the Microsoft Store

Once the application has opened type python in the search bar and select the desired version.

Installing Python via the Microsoft Store
Type Python in the searth bar and off you go

once your selected version has loaded simply click “Get” and let it do its thing.

Install Python via the Microsoft store
Installing Python from the MIcrosoft Store

There is no indicatoin of progress with this form of installation that can to be fair seem a little disconcerting; but once it is installed you will receive the following.

Python installed by the Microsoft Store
well that was simple.

Linux

For completeness I have also showed how to install python on a Linux environment, this is ubuntu running under WSL on my windows box, but the steps should work on full fat Linux distributions too.

To install Python from the command line start with the command:

$ sudo apt install python3 python3-pip

The above command will install the latest version if you wish to specify a particular version for example Python3.6 use this command:

$ sudo apt install pythons3.6
python installation error
Well that was not expected. seems that I already have a version installed.

As I already have Python installed in my environment it will not be installed again. However you will note that I am being asked to removed certain packaged that are no longer required.  To clean up our environment let us carry out that request.

Cleaning up the mess
So now we can clean up the mess

Finally let’s verify our installation by rerunning the installation routine.

everthing is sorted now on our Python installation.
a Quick verification everything is working on our python installation.

Yep we are, next we need to add the PATH to bashrc. Go to your home directory again and open the .bashrc file. Then add the following line at the end of your .bashrc file:

export PATH=”$PATH:/home/your_linux_username/.local/bin”

In my case it was —

export PATH=”$PATH:/home/tomh/.local/bin”

Python should now be correctly installed.  Next let’s test it by writing the most simple of python scripts and running it to see if Python works or not.

Our first programme

It is time to write our first program using Python, in time honoured fashion, this shall be our announcement to the world. Or to be more precise the venerable “Hello World”.

Open up your vsCode programme and select your desired folder,

opening python in vscode
creating an environment for writing python in VS Code

As this is a new series focusing on a different language, I created a folder called “python” and created a sub-folder called “helloWorld”.  Next, create your first file, I called it “helloWorld.py”; “.py” being the extension type for python files.

We are going to write the simplest of programs.

Print(“Hello World”)

To run the file in your terminal enter the following command:

python helloWorld.py

The first word is to invoke the python interpreter, the second part is the python script you wish to run.  On a successful invocation, you will receive the following response:

Running your first python script
Success! our first python code

Next we will write something a little interesting (not that much more interesting through 😊)

Create a new file in VS Code and this time I called it “count.py”  enter the following into the file

1 Def printList(lst):
2     For items in lst:
3         Print(item)
4
5 Lst = [1, 2, 3, 4, 6]
6 printList(lst)

If you have been following my Golang posts you will recognise a “for” loop, yes the syntax is slightly different but the concept is the same.

In this code we have defined a Python function called “printList” it takes a single argument called “lst”, we know this as we have used the “def” keyword. The “def” keyword acts in a very similar manner to the “package” keyword in golang.  The purpose of this function is to iterate over the items in a defined list which is shown on line 5. “lst = [1, 2, 3, 4, 6]”, this is done with the “for” statement that reads in the list.  The final line prints the list to stdout with the line “printList(lst)”.

Running this code should result in the following output

it is a simple as 1, 2, 3, 4, 6. this python code is
Python Code is a simple as 1,2 3, 4, 6!

Before we finish I think a little conversation about the formatting of Python code is in order,  if you write Terraform or Golang you will be used to enclosing your blocks of code in brackets (braces) for example

provider "azurerm" {
  features {}
}

Python uses indentation rather that curly brackets (braces), you can use either tabs or spaces to indent your code.  If you chose to use spaces there is a requirement to use four spaces per indentation level.

Summary

Now that we have installed python and confirmed that the installation is working as expected by writing two very simple pieces of code.  In our next article we will start to learn the basics of variables, types, lists, basic Operators and the other building blocks of python.

 

NEWSLETTER

Receive our top stories directly in your inbox!

Sign up for our Newsletters

spot_img
spot_img

LET'S CONNECT