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

This is the first exercise:

I. Create a script that does the following

    A. Prompts the user to input an IP network.

        1. For simplicity the network is always assumed to be a /24 network

        2. The network can be entered in using one of the following three formats, 10.88.17., or 10.88.17

    B. Regardless of which of the three formats is used, store this IP network as a list in the following format ['10', '88', '17', '0'] i.e. a list with four octets (all strings), the last octet is always zero (a string).

        Hint: There is a way you can accomplish this using a list slice.

        Hint2: If you can't solve this question with a list slice, then try using the below if statement (note, we haven't discussed if/else conditionals yet; we will talk about them in the next class).

>>>> CODE <<<<
if len(octets) == 3:
elif len(octets) == 4:
    octets[3] = '0'
>>>> END <<<<

    C. Print the IP network out to the screen.

    D. Print a table that looks like the following (columns 20 characters in width):

      NETWORK_NUMBER   FIRST_OCTET_BINARY      FIRST_OCTET_HEX      0b1011000               0x58

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

ip_add = input("Enter an IP address: ")

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:

[email protected]:~/python/Week2$ python3 
Enter an IP address:

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

ip_add = input("Enter an IP address: ")

This produces the following result:

[email protected]:~/python/Week2$ python3 
Enter an IP address:

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.

ip_octet = ip_add.split(".")

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

ip_octet = ip_add.split(".")

This then gives the following input:

[email protected]:~/python/Week2$ python3 
Enter an IP address:
['192', '168', '10', '0']

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:

ip_octet = ip_octet[:3]

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:

[email protected]:~/python/Week2$ python3 
Enter an IP address:
['192', '168', '10', '0']

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.

ip_new = ".".join(ip_octet)

print("Your network is: {}".format(ip_new))

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:

[email protected]:~/python/Week2$ python3 
Enter an IP address:
Your network is:

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...

[email protected]:~/python/Week2$ python3 
Enter an IP address:
Your network is:
Traceback (most recent call last):
  File "", line 17, in 
TypeError: 'str' object cannot be interpreted as an integer

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.

first_octet_bin = bin(int(ip_octet[0]))
first_octet_hex = hex(int(ip_octet[0]))

print("{:<20} {:<20} {:<20}".format("NETWORK_NUMBER", "FIRST_OCTET_BINARY", "FIRST_OCTET_HEX"))
print("{:<20} {:<20} {:<20}".format(ip_new, first_octet_bin, first_octet_hex)) 

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:

[email protected]:~/python/Week2$ python3 
Enter an IP address:
Your network is:

NETWORK_NUMBER       FIRST_OCTET_BINARY   FIRST_OCTET_HEX         0b11000000           0xc0            

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

    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


Leave a Reply

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