In this lesson we’re going to go over some Python concepts. We’re going to do an overview of Python, talk a little bit about why Python was selected by Juniper for inclusion in this certification, go over some of the main features of Python and then also go through some live scripting examples so we can take a look at how Python works in action. So, without further ado, let’s jump right on in!
So first up, why was Python selected as the scripting language to be included in this certification? Well, Python is easy to read. That is one of the main features of Python that the developers of the language were looking to achieve with this scripting language. it uses white space for structure and to enforce readability. Because it uses indentation and white space for structure, it forces our code to generally be formatted very similarly between developers. As you could imagine, if there was no need for indentation in your script or programming, then you as the developer could choose to format your code in any which way and it could be substantially different from developer to developer. However, because Python does use white space for structure then we are forced to generally format it the same across platforms and between developers.
Python is already heavily used in DevOps. Ansible and Salt are both written in Python, and many networking vendors have chosen to use Python for automation. Juniper already allows python to be used for both off-box and on-box scripting, and libraries have been created by Juniper for Python to make things easier for us as the developers. Starting with some of the later Junos releases we can actually create Python scripts that execute locally on our Juniper device. Finally one of the benefits is that there is a very large community around Python. With all of the information available there are lots of tools and packages and lots of learning material available. It’s a very very popular language, one of the world’s most!
Let’s talk about some of the primary attributes of Python. First up it is an interpreted language. This means that it is compiled at run time, rather than ahead of time. This does have some benefits, but also some drawbacks. In the event there’s an error in your code that perhaps is syntactically correct but does not do what is expected, then you may not necessarily know that exists until that piece of code is run, which might not occur unless it’s a very arbitrary set of circumstances in your code.
Python is an object-oriented scripting language. There are two main versions of Python in use, Python 2 and Python 3. Python 3 is what is currently and fully supported. Python 2 is largely deprecated, though it is still in use in many places. If you’re just getting started, I highly recommend starting with Python 3. There are some subtle differences here and there, and some larger differences as well.
Here are some basics around Python. This is really just how to do some basic operations, some assignments and some mathematical operators. Let’s go ahead and step through these and talk about the difference between how Python 2 and Python 3.
First up, an assignment. So variables are what’s called dynamically typed, which means that they take on the type of data that you put into them. You do not have to specify ahead of time that a our variable ‘a’ is an integer or that our variable ‘a’ is a float. You can just assign an integer or assign a floating point number to ‘a’ and it will automatically take on the appropriate type. Same thing with string or a list type or a dictionary which we’ll get into those a little later.
So an example here though of a difference between Python 2 and 3 is that when assigning a string to a variable, Python 2 results in an ascii string, whereas Python 3 will result in the variable being of type unicode string. It’s a subtle difference, if you’re not aware of the difference between ascii and unicode, it’s a different character and coding method. Go ahead and take a look on Google for more information on the character encoding differences.
Another item is that Python 2 will only output the same type which was input to an operator. So if I’m performing an operation on integers, it will result in an integer. Results will be truncated if the result is not an integer. For example we have 2 divided by 3 which is you know 0.666666 etc.
In Python 2, our result here would be 0, because we are only inputting integers into our operation and therefore the output will be truncated and give us an integer output. Python 3 however will automatically output whatever number type is required, so if the resulting calculation is a floating point number then it will result in that floating point rather than just truncating the answer.
So let’s go through some of these operators, a couple of these might not be real obvious if you’re not used to programming or scripting. First up, our simple addition. Hopefully we all know what addition is, and our simple subtraction.
Now, the % sign is called the modulo operator. This returns the remainder from division. So, 4 % 3 equals the remainder from 4 divided by 3. This division results in 1 and a remainder of 1. Modulo can also be thought of in a clock sense. Imagine we have a clock with three numbers on it, 1 2 3 and we are going around four steps starting from one. We end up back at the one. It can just be a different way of looking at modulus algebra.
Next we have of course our simple multiplication. In this case a will result in an integer 6. Then our simple division, in the case of 5/2, Python 2 will just output 2, because this results in 2.5 and Python 2 will truncate that number and give us just 2 integer because we are putting in integers to our division, whereas Python 3 will automatically give us our 2.5 floating point number.
Finally the way that we show exponents in Python is with ** or 2 asterisks. This example above could be read as “2 to the power of 3”, which if you’re unfamiliar with powers that would be 2*2*2, which ends up equaling 8, right because we’ve got 2*2=4 and then 4*2=8.
Next we’re going to go through some scripting examples and with this I’m going to use a Jupyter notebook. Jupyter notebooks are a really handy tool for providing some markup and having effectively a notebook for your scripting testing and to show some live information and to be able to keep your scripts and provide a lot of comments to them in a nice easy to handle kind of way. Jupyter notebooks is actually a Python application, this is installed using the pip command pip install notebook. pip is the Python package manager, when you install Python on your computer you’ll want to install pip as well. Then, from a command line you should be able to run pip install notebook and that will install Jupyter notebooks for you. From there you can launch your Jupyter notebook by using the command jupyter notebook.
It may differ slightly if you’re on a different platform like Windows, MAC or Linux. I’m using my Windows computer. This lesson’s notebook is available for download below. For a full video lesson and live scripting example, please subscribe and view our video lesson below!