
Python is a great programming language for Network Engineers. It will help them take their career to the next level in the networking field.
Below are a few topics we will be discussing:
Python Comparison Lists and Tuples
Python Comparison Operators
A major comparison between a Tuple and a List
Lists in lists
Python List Methods
Commenting on Python Code
How to wrap Python lines long?
Here is the training video on Commenting Python Codes (for network engineers). Comment on this video.
Python Comparison Operators
They are also known as Relational operators.
Operation Meaning Strictly Less Than =Less Than or Equal >Strictly Greater than >=Greater or Equal ==Equal!=Object identify is notNegated object identification
For example:
>>>a= “Python”
>>>a== ‘Python’
True
>>>a = 10
>>> (a==10) oder (a!=5)
True
>>> (a==10) & (a!=5)
False
A major comparison between a Tuple and a List
Lists and tuples are both sequence data types that can store a number of items. Access to any item can also be done by its index. Each item in a tuple, or a list can be any data type.
The main difference between a list and a tuples, however, is that lists can be modified (we can overwrite or add elements to them), while tuples cannot.
Lists in lists
You can create a list by placing all items (elements), inside a square bracket [], separated with commas. It can contain any number of items, and they can be of different types (string, integer, floating, etc). A list can have an item from another list.
>>> hostnames = [‘cisco-r1′,’cisco-r2’]>>> type(hostnames)
>>> IPs = [‘1.1.1.1′,’1.1.1.2’]>>>type(IPs)
>>>
>>>Devices = [hostnames, IPs]>>>Devices
[[‘cisco-r1′,’cisco-r2’], [‘1.1.1.11′,’1.1.1.2’]]>>>
>>>Devices[0][‘cisco-r1′,’cisco-r2’]>>>Devices[1][‘1.1.1.11′,’1.1.1.2′]>>>Devices[1][1]’1.1.1.2′
>>>Devices[0][0]’cisco-r1’
Python List Methods
Method Description Python List append )Add one element to the end. Python List extend ()Add elements to another list. Python List insert ().Insert elements from the Python List removed ().Returns the smallest index of an element in Python List count ().Returns occurrences in Python List pop ().Removes elements at given index Python List reversed.
>>>hostnames.append(IPs)
>>>hostnames
[‘cisco-r1′,’cisco-r2’], [‘1.1.1.11′,’1.1.1.2’]]
>>>hostnames = [‘cisco-r1′,’cisco-r2’]>>> IPs = [‘1.1.1.11′,’1.1.1.2’]>>>hostnames.extend(IPs)
>>>hostnames
[‘cisco-r1′,’cisco-r2’], [‘1.1.1.11′,’1.1.1.2’]
>>>hostnames = [‘cisco-r1′,’cisco-r2’]>>>hostnames +=IPs
>>>hostnames
[‘cisco-r1′,’cisco-r2’], [‘1.1.1.11′,’1.1.1.2’]
Commenting on Python Code
Commenting on your code helps to explain your thinking process and allows you and others to better understand the code.