Site icon Karneliuk

CEX (Code EXpress) 02. Your first Python code.

Hello my friend,

I was thinking what the next thing shall be right after you have installed the Python. Initially I was thinking about another topic, but decided to get some fun for you, as doing something is definitely fun.

Network automation training – boost your career

Don’t wait to be kicked out of IT business. Join our network automation training to secure your job in future. Come to NetDevOps side.

How is the training different from this blog post series? Here you get the basics and learn some programming concepts in general, whereas in the training you get comprehensive set of knowledge with the detailed examples how to use Python for the network and IT automation. You need both.

What are we going to do today?

Today is a big day in your Python journey, mate. Today you will create and execute your first Python code. It will be simple and will do only one task: it will print fixed text. However, it has an enormous value for your learning curve in programming.

Why does it matter?

Despite it looks very easy, there are a couple of aspects, which are important for your further progress:

  1. Knowing how to create the Python file and what to include inside it is a mandatory part of any program, what is, obviously, applicable for the network automation as well. So, you will get that understanding.
  2. After the file is created, you need to know how to execute it. If you are not careful, you might execute it with a false Python version, as legacy Python 2.7 is installed by default in almost any Linux (including our CentOS 7). In the best case, the execution will be stopped by the error. However, the outcome could be even worse. There are multiple ways how you can execute the Python code, and you will learn a couple of the most used approaches. 
  3. You will get the first impression on working with functions. Functions are building blocks of any programming language, and Python is not an exception. Later on, in our series you will learn how to create your functions, but for now you will familiarize yourself with a format.

As you see, my friend, even the things, which might look simple at a glance, have a very big value.

How are we doing that?

First of all, we need create a folder, where we will store the code for the examples, so we do it as follows (assuming you are in your home directory):


1
2
3
4
5
$ mkdir CEX
$ cd CEX
$ mkdir 02
$ cd 02
$

More details about working with Linux you can get our network automation training.

Once you are in the proper directory, you can keep calm and focus on the Python code. You are about to create your first own Python code. There are no particular naming conventions to the files in the Python you should follow, so you can take any random name you like. We advise to provide some meaningful names so that you can understand what’s that about. However, there is a strong advice to add the extension .py to all your Python files to allow them to be cross-platform and ease the working with your code for somebody else. Let’s call the file for this blogpost printing.py. Per the task, the Python script shall be printing fixed task, so your file would look like: 


1
2
3
4
$ cat printing.py
#!/usr/local/bin/python3.8

print('Hello, new network automation mate! This exciting world is waiting for you.')

Here is the step by step explanation what is happening:

The first line has a specific starting charset “#!” called shebang. The shebang instructs the executable file (more on that later today) that it shall use the code interpreter following by this shebang symbol. In the case above, the path for the interpreter is “/usr/local/bin/python3.8”, what is the path where you have installed Python3.8.1 by default. The reasonable question is where we get this path from, as we have not defined that in the previous blogpost. To get it, you should run the following command before you create your Python script:


1
2
$ which python3.8
/usr/local/bin/python3.8

The second line of the code is empty. The good point is that you can add any amount of the empty lines per your taste, as they don’t impact somehow the code’s execution.

The third line contains the execution of the function with the parameter. In Python any function is provided in a form “function()”. If the function has arguments, it is provided as “function(arg1, arg2, .., argN)”, where the amount of the arguments are depending on the function internal structure. In the provided example, your function is “print()”, which may have from zero up to (virtually) indefinite amount of the arguments. You have provided a single argument, which contains a string contained in the single quotes. The outcome of the script’s execution shall be the string to your Linux CLI (called STDOUT – standard output). Let’s verify that:


1
2
$ python3.8 printing.py
Hello, new network automation mate! This exciting world is waiting for you.

You see the CLI that the string is printed as expected. Now, we have did a small trick by putting “python3.8” in front of the script’s name. This forces the script to be processed by the python3.8 interpreter explicitly despite the path of the shebang. You can potentially break the code’s functioning (at least partially) if you provide the faulty interpreter:


1
2
3
4
$ python -V
Python 2.7.5
$ python printing.py
Hello, new network automation mate! This exciting world is waiting for you.

Don’t repeat in the production, but that is OK to do that in lab.

In this case the script worked properly. However, in many cases the Python 3.* is not backward compatible with the Python 2.*, so you might break some code’s execution. That is especially bad if that is somewhere in the middle of the execution, as you might have performed some network changed by the point you reach the problem, so the rollback could be very difficult. 

In order the path to the interpreter after the shebang is used, you need to make the file is executable. To do that, you need to change the file’s permissions as follows as the files aren’t executable by the default:


1
2
3
4
5
6
7
$ ls -l printing.py
-rw-rw-r--. 1 aaa aaa 113 Feb  3 21:00 printing.py

$ chmod a+x printing.py

$ ls -l printing.py
-rwxrwxr-x. 1 aaa aaa 113 Feb  3 21:00 printing.py

The initial output of the command “ls -l” provides the list of the files permissions as “rw-rw-r–“:

After you change the permissions to add the execution, using “chmod a+x”, the permissions are changed to “rwxrwxr-x”, meaning that anyone is able to execute this file now. This gives you an option to run the file without mentioning the interpreter explicitly:


1
2
$ ./printing.py
Hello, new network automation mate! This exciting world is waiting for you.

And that is it for today. Our learning objectives are achieved.

If you prefer video

If you prefer watching the video instead of reading blogpost, that is also wonderful. We value your time and want to create for you the best experience possible. Therefore, for all the blogposts associated with CEX series, we will record the video and post it to our YouTube channel.

And here is the video for this blogpost.

What else shall you try?

Programming is all around testing, trying, breaking and fixing the things. Try to do the following things to collect some more experience:

Lessons in GitHub

You can find the final working versions of the files from this blog at out GitHub page.

Conclusion

Despite this is just the second blogpost, you have already created your first script and have learned some things about Linux and Python code’s execution. Going step by step in such a manner will give you an understanding of Python and some time to practice around the basics. Take care and goodbye!

Support us






P.S.

If you have further questions or you need help with your networks, I’m happy to assist you, just send me message. Also don’t forget to share the article on your social media, if you like it.

BR,

Anton Karneliuk 

Exit mobile version