This post will describe the exercises and solutions for week two of Kirk Byers Python for Network Engineers.

This is the first exercise:

To get input from an user we need to use the “input()” function. In legacy Python the function is called “raw_input()”.

The result of what the user inputs is stored in the variable “ip_add”. The text within the paranthesis is what we want to display to the user so that we get input back from the user. This is what the script looks like so far:

Let’s first print out the variable and check that it’s a string.

This produces the following result:

The next step is to store the IP network as a list consisting of strings. We will use the built-in function “split()” to split the IP address into octets and tell Python that a dot is the delimiter.

We can also print the contents and check that we have a list.

This then gives the following input:

We were told though to only type in the first three octets so currently we have no control of what the user types in the fourth octet. We want to discard that part. We can use list slicing to accomplish this. When using slicing it is zero indexed so the first string in the list is 0, the second one is 1, the third one is 2. It’s also important to note that the end index is non inclusive so if the end index is 3, only index 0, 1 and 2 will be included. The syntax to use list slicing is “listname[startindex:endindex:stepping]”. Our code then becomes the following:

The syntax here is that we want to start at index 0, this is indicated by “:”, the last index is 3, non inclusive so the last one to enter the list is actually 2. It would also have been possible to do “ip_octet = ip_octet[:-1]”. This would have ignored the last string in the list(“-1”) but our current code is better since it would protect against the user entering more octets than four.

The next step is to add a string to our list. Adding something can also be called appending. There is a built-in function called “append()” which will help us here. We will add the string “0” to the list.

This is what the variable ip_octet consists of right now:

This looks good so far. The next task is to print the network out to the console. We need to use “join()” to join the octets back into an IP address. We will insert a dot between the strings when joining them.

I’m using Python 3.6 syntax here. For older versions the “{}” might have to be indicated as “{0}” and legacy Python uses “%s” for strings instead of the newer “.format” syntax.

This looks good so far:

The next step is to print a table with the network number, first octet in binary and first octet in hex. There are built-in functions to convert integers into binary or hex. However, currently we only have a string. Let’s try to print the binary number of our network number.

This however doesn’t work so well…

The string must first be converted into an integer. The code to do this is “bin(int(string_name))”. We also want to print a pretty table. We will use “.format” to format and make it left aligned. This is indicated by “{:<20}" in the code where the "<" indicates left aligned and 20 is the width of our column. This is then the code we need to put in.

We store the first octet in binary and hex in the new variables. We use the built-in functions "bin()" and "hex()" but we run them through "int()" since we had a string to beging with. As you can see it's possible to run a function in Python on input from another function. We are using list slicing to pick out the first string from the list "ip_octet". We then print some "newlines" and then print the table referring to the variables above. The final result is then this:

The code is as usual available at my Github. See you next time!

Python – Kirk Byers Course Week 2 Part 1

One thought on “Python – Kirk Byers Course Week 2 Part 1

  • April 1, 2017 at 12:06 am
    Permalink

    This iis very fascinating, You’re an excessively professional
    blogger. I have joined your rsss feed and sta up for looking for more of
    your great post. Also, I’ve shared your website in my
    social networks

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: