Archive

Posts Tagged ‘for loop’

Learning Python: Week3 (Conditionals and For Loops) -Part 2

14 May 2017 Leave a comment

As discussed in post  ( https://crazyrouters.wordpress.com/2017/02/25/learning-python-kirk-byers-python-course/  ) , i will be sharing the my learning on weekly basis as course continues. This will not only motivate me but also help others who are in phase of learning python 3.

This post will focus on Week 3 (Conditionals and For Loops) .This post will focus on exercise 1

######################## EXERCISE 1 #######################

I. Create an IP address converter (dotted decimal to binary).  This will be similar to what we did in class2 except:

    A. Make the IP address a command-line argument instead of prompting the user for it.
        ./binary_converter.py 10.88.17.23

    B. Simplify the script logic by using the flow-control statements that we learned in this class.

    C. Zero-pad the digits such that the binary output is always 8-binary digits long.  Strip off the leading ‘0b’ characters.  For example,

        OLD:     0b1010
        NEW:    00001010

    D. Print to standard output using a dotted binary format.  For example,

        IP address          Binary
      10.88.17.23          00001010.01011000.00010001.00010111

    Note, you might need to use a ‘while’ loop and a ‘break’ statement for part C.

        while True:
            …
            break       # on some condition (exit the while loop)

    Python will execute this loop again and again until the ‘break’ is encountered. 

############################ END ##############################

 

Most of the part of this exercise is repeated one from last exercise done in week 2.

Lets take step by step , first we need a command line argument instead of prompting from the user, this can be done be sys,argv (discussed in last post )

 

scriptarg1

 

Here we are getting input from command line, restricted input not more than 2 argument.if we tried to give more than 2 argument from command line, it will through a error.

Output:

scriptarg1_out

Now we need to split the entered IP address using split method and then convert into binary. Its same as we did in week 2 exercise.

raw_input=ip_addr.split('.')

 

Above code will split the ip address into element of list separated by ‘.’ .

We will also define a list which we will be using afterward to store the ip octet converted into binary.

ip_add_new =[]

 

We will use the for loop to go through each octet of Ip address and convert into binary

for i, element in enumerate(raw_input):
 octet = (bin(int(raw_input[i])))

we will get the output as below

0b11000000
0b10101000
0b1010
0b1

Its the Binary conversion of each octet of ip address. So we are done with part a and b of exercise .Now we need to zero-pad the digits such the binary output is always 8-binary digits  longs i.e strip off the leading ‘ob’ characters from above output.

Lets first remove the ‘ob’ from each binary converted octet ,this can be easily done by string slicing.

octet_new = octet[2:]

 

octet_new will have now have binary representation of ip octet without first two character of string i.e ‘ob’ that are at index 0 and index 1.

In short , now octet_new will have following values

 0 octet   11000000 
 1 octet   10101000 
 2 octet   1010 
 3 octet   1 

so ‘ob’ is removed from each octet’s binary output.

Now we have the make each octet with zero-pad such that binary output is eight digit.

so for that we run while loop , it checks for the length of octet and gets output as if condition fails (len(octet_new) >=8:).

if condition true , it will run the while loop adding the ‘o’ to the octet binary till length of binary output becomes 8.I also need to append the element into the list which i have defined earlier i.e ip_add_new =[] with each iteration.

 

 

for i, element in enumerate(raw_input):
octet = (bin(int(raw_input[i])))
print(octet)
octet_new = octet[2:]
print(octet_new)
while True:
if len(octet_new) >=8:
break
octet_new = '0'+ octet_new
print(octet_new)
ip_add_new.append(octet_new)

 

so only  thing to focus is to join all four octet of binary representation using ‘.’

that can be easily done using  ‘.’.join method

ip = '.'.join(ip_add_new)

Now print the output using format method

 

print("\n")
print("{:20}{:20}".format("IP address","Binary"))
print("{:20}{:20}".format(ip_addr,ip))

Here is the Code from scratch for this exercise.

exercise1.png

OUTPUT:

exercise1_out

So here we are done with the Exercise 1 , will be back with exercise 2.

Smiles 🙂

     

 

 

%d bloggers like this: