Learning Python: Week2 (Printing, Numbers, and Lists) -Part 1

30 Apr 2017 Leave a comment

As discussed in last 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.

Kirk discussed about the printing ,raw_input,numbers,List and Tuple . Few of my friends asks to include  summery in form of notes about whatever  learned from course ,then further proceed with exercise.

So i will be sharing the  very short notes before  proceeding with the exercise

 

NOTES:

 

Kirk talks about the format syntax to print and method to get the input from the user.

 

Format method used to print

 

format

We can also provide the input in any sequence and call it accordingly in print statement as in example below:

format1

{1}  – calls input provided as sequence 1 i.e “ Dubes”

{0} – -calls input provided as sequence 0  i.e “30”

{3} – -calls input provided as sequence 2 i.e “Delhi”

 

Input method to get the dynamic Input from the user

Script:

format3

Output:

format3_output

 

Numbers :

Already discussed about the string method split in week 1 ,we can convert each octet of IP address into binary and hexadecimal as below

number

As above, we are converting integer 172 into binary ,not string ‘172’ ,as ip_add is list having values stored as string , so we need to convert the same into integer to make binary and hex function to work upon them.

>> bin(int (ip_add.split(‘ . ‘)[0]))

so we have changed string type into integer.Below snapshot show example to covert ip_add octet into binary and hex.

number1

 

List :

If we want to remove the list element , we can go ahead with the help of pop ,remove and del.

POP: Removes the list element based on index, it reflect the pop value in output.

Remove: Removes the list element based on value not index.

Del :Deletes the list elemet based on index

pop

Note : ****List is mutable means that list value can be changed. If a and b is pointed to same list , if a element is change in b , there will be change in element b as a and b  both points to same memory.

Let’s have a example for the same.

list

So how to go for scenario , where we need to change the element of list a , but not element b ?

We can use slice method to fulfil the above requirement.

 

slice.png

 

So, i am finished with the Notes .Next post i.e Part 2 will  have the exercise related to whatever we have covered in Notes.

 

 

 

 

 

 

 

 

 

 

 

 

Categories: NOTES, Python Tags: , ,

Learning Python: Week1 (Preliminaries and Strings)

15 Apr 2017 Leave a comment

 

As discussed in last 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.

Kirk discussed about the string methods in first week ,where he also put some exercise for us to work upon.The exercise seems to be simple for python experts ,but its  good learning exercise for the beginners who are in process of learning Python.

This post will focus only on the python exercise  for week 1.

Exercise:

1 ) Need a python script where user echo an input as IPv4 address and our task is to split into its octets.

 

Code

1c

Output

1o

 

We have used the system specific function  defined to get the user input and print the output. Sys.stdin is used for all interactive input by user whereas sys.stdout is used for the output of the same.

Here we are using echo to send the data to sys.stdin .We have entered input as  “Hello ! How are You” .Which gets print out the text on screen.

We can also used the fileinput.input() function to read the file or interactive input by user, same shown in below code.

We have to modify the code so that user enter the Ipv4 address and get the Ip address to be split into octets

 

Code

2c

Output

2o

 

We have used string method line.split() to split the ip address into octets. But here comes the new problem ,we don’t want newline character ‘\n’ in output. We need to get rid of the same.

 

Code

3c

As in above mentioned code ,I tried to used string method string.rstrip(‘\n’) on the output list ,but it gave error ‘list object has no attribute ‘rstrip’

That means list does not support rstrip().

So it makes us to think further other method to get rid of newline character ‘\n’. There are two ways to achieve the required result.

 

Code-way1

4c

Output

4o

Above code has used  the rstrip() method on the specific  element of the list  rather than whole list

 

Code-way2

5c

Output

5o

 

We have modified the code as shown above  , we are working first to get rid of the newline character by using the rstrip() method on the initial  string input by user and then going for the strip of the ip address into different octets resulting output as a list . It Seems more straight forward 🙂

 

Exercise 2

 

  1. Use the split method to divide the following IPv6 address into groups of 4 hex digits (i.e. split on the “:”) FE80:0000:0000:0000:0101:A3EF:EE1E:1719 and use join method to reunite your split IPv6 address back to its original value.

 

Its bit simple , if you gone through last exercise . It is a easy part just need to use the split() and join() method to do the needful.

 

Code:

6c

Output :

6o

 

 

 

Learning Python -Kirk Byers Python Course

25 Feb 2017 1 comment

Learning is process and there is no end to it. I came across Kirk Byers free python series and subscribed to the same .

The next Learning Python course starts on April 13th. I should receive the first class on that day. Reviews for this course is positive .Hopefully i will be able to follow the course as schedule and would share my learning as the course  proceeds.

About Kirk Byers :

He is  CCIE (emeritus) in routing and switching and have extensive Python experience. Since 2014, he has  been teaching Python and Ansible to network engineers. he also writes the Netmiko Python library and contributed the IOS driver to NAPALM.

https://pynet.twb-tech.com/

 

Categories: Python

Python Functions – Basic

11 Dec 2016 Leave a comment

Python Function is a block of statements that can be used multiple times in a program. Its tedious task to use same block of statements multiple times, instead we can have function which contains same block of statement and can be called wherever there is need for the same.

A function in Python is defined by a def statement. The general syntax looks like this:

Def functionname( parameter list):
                                       Statements
                                        Return

 

 

Optional parameters

Function can be called by using the following arguments

1 Default Parameters

A default argument is an argument that takes a default value if value is not provided in function.

Program:

1

Output :

2

 

2 Variable-length arguments

This is used in case where we need to process a function for more arguments than specified while defining the function.

These are variable-length arguments and are not named in function definition.

Program:

3

Output :

3out

 

3 Anonymous

These functions are anonymous because they are not declared in the standard manner by def keyword

Lambda keyword is used to define small anonymous functions

Program:

4.PNG

 

Output :

4out.PNG

 

  1. Return function

Statement return exits a function , optionally passing back an expression to call.

Program :

5.PNG

Output:

5out.PNG

 

Local and Global Variable

Variables that are defined inside a function has local scope whereas defined outside function has global scope

Program:

5.PNG

Output:

5out 

 

 

 

 

 

 

 

 

 

 

 

 

ARP – Address Resolution Protocol

9 Dec 2016 Leave a comment

When ever Network Engineer thinks  of ARP , first thing which comes in his mind is MAC address and layer 2.

MAC  Address is unique identity provided to NIC card for communication at layer 2. It’s also sometime referred as Burned-in-address or hardware Address.

Every network engineer should have good understanding of ARP. ARP is used to map  IP address to MAC-address used by data link protocol.

How ARP works , we will take a simple scenario to explain the concept of ARP

 

1

PC1 (IP 192.168.1.10) wants to communicate with PC2 (192.168.2.10).

Lets first have a look at ARP header to have clear understanding of ARP fields.

2.png

Hardware type is of 2 byte which is network protocol type: in this case , it’s Ethernet which is 1.

Protocol type field is the internetwork protocol for which the ARP request is intended. In this case it’s IP, this has the value 0x0800.

Hardware Address length indicates length of hardware address, in this case Its Ethernet so hardware size is 6 bytes.

Protocol Address length indicates length of protocol address, In this case, its IP so protocol address length is 4 bytes.

Opcode is operation code that the sender is performing: 1 for request, 2 for reply, 3 and 4 in case of RARP(reverse ARP)

Sender protocol and Target protocol address is source and destination IP address.

Source hardware and Target hardware address is source Mac-Address and Destination Mac- address.

Initially, there is no ARP entry present on either of the PC, means that neither of the PC  or R1 is having any information of destination MAC address which is necessary for each other communication.

ARP entry of PC1 ,PC2 and R1 at initial phase

PC1

3

PC2

4

R1

61.PNG5.png

 

Now lets  initiate a ping request to PC2 from PC1 , as PC1  does not know the MAC address of router so , so first frame is broadcast asking  Who has 192.168.1.1 ? Tell 192.168.1.10 (PC1) . If you sees the first ping packet , its generally dropped ,it’s because of same reason.

6.png

Let’s have a look at detailed ARP capture

PC1 – Broadcast Frame

7

In this capture , we can see that ARP packet with Opcode =1(request) , with sender MAC, target MAC and Sender IP address, but as PC 1 is not aware of Target MAC address, so it puts broadcast address (ff:ff:ff:ff:ff:ff) for Target Mac address and broadcast the frame.

First frame is broadcast asking  Who has 192.168.1.1 ? Tell 192.168.1.10

Router R1 configured with the IP address 192.168.1.1 sends a reply which is unicast to source PC1 . Let’s have a look of the ARP capture for unicast frame

PC1 – Unicast Frame

8.png

Above ARP frame  capture is having Opcode as 2  i.e reply. The sender IP is now 192.168.1.1 (R1 ) and destination IP is 192.168.1.10 (PC1) ,accordingly R1 unicast the frame with source MAC  of itself and destination MAC of PC1.

R1 also saves the source MAC address in its ARP cache.

Thus overall process makes PC1 aware of the destination MAC of gateway IP (R1)

 

Similarly same processs is done at PC2 end to get the IP address resolve to MaC address. ARP  request is broadcast from PC2 for destination Mac address of 10.10.2.1 and unicast packet with required details is received from R1 .

Below is Wireshark capture for PC2 for ARP frame (for broadcast and unicast ) for reference.

PC2 – Broadcast Frame

9.png

PC2- Unicast Frame

10.png

Thus  PC1 and PC2 are now  aware of the respective gateway hardware address. R1 now being too aware of PC1 and PC2 MAC address thus makes communication possible between PC1 and PC2

 

ARP entry of PC1 ,PC2 and R1 after successful ping.

12.png

13.png

14.png

 

Hope this post have cleared your any doubts regarding ARP   🙂 .comments are Welcome !!!

==========================================================================

Be updated about the Telecom news and new Technology .

Like : https://www.facebook.com/crazyrouters/?hc_ref=PAGES_TIMELINE&fref=nf

Python Loops – Basics

7 Dec 2016 Leave a comment

 

The if, while and for statements implement traditional control flow and provide the output depending upon the condition.

 

IF /ELSE

 

An if statement consists of a  condition followed by one or more statements.An if statement can be followed by an optional else statement.

Else statments is used with for loop to give the result when loop has exhausted  iterating the list.

If else stament is used with a while loop,the else  statment is executed when the condition becomes false.

CODE:

if

 

OUTPUT:

if_out

 

While Loop

It’s an indefinite loop which keep iterating until some condition is met. There is no                guarantee regarding how many times the loop will go round .

While <condition >

<Body>

 

while1

CODE :

while

OUTPUT :

whileoutput

Good use of indefinite loop is to have interactive loop. This loop help us to go through file data.We will have program using all the above mentioned terms in future posts

 

For Loop

Its iterate over the items of any sequence as a list or string.

for-loop

 

CODE:

for

OUTPUT:

for_output

Use of range with for loop 

CODE:

for1

 

OUTPUT

for1_output.png

 

Get Trained on the AWS Cloud

2 Dec 2016 Leave a comment

Get Trained on the AWS Cloud

AWSome Day Online Conference is a free, online training event that will provide a step-by-step introduction to the core AWS services for compute, storage, database and networking. AWS technical experts will explain key features and use cases, share best practices, walk through technical demos, and be available to answer your questions one-on-one.

Based on the AWS Essentials Course, AWSome Day is ideal for IT managers, business leaders, system engineers, system administrators, developers and architects who are eager to learn more about cloud computing and how to get started on the AWS Cloud.

Why you should attend:

 

  • Gain a deeper understanding of AWS core and application services
  • Learn how to deploy and automate your infrastructure on the AWS Cloud
  • Get your questions answered by our AWS experts
  • Receive a Certificate of Attendance when you complete the all modules

Register on below mentioned link:

https://aws.amazon.com/events/awsome-day/awsome-day-online/

 

Date: 6 December 2016
Time: 10am – 1.30pm IST
Location: Online

Categories: Cloud, Uncategorized Tags: , , ,
%d bloggers like this: