Cisco ACI and CCNA live online batches are starting from 24th June 2023. Join experts today. Python List Methods Archives - UniNets Blog

Tag Archive Python List Methods

Commenting Python Code- Python for Network Engineers

Python is one of the best programming languages for Network Engineers and demanding which will take their career a step ahead in networking field.

Given below are few topics on which we are going to discuss about:-

  • Python Comparison Lists and Tuples
  • Python Comparison Operators
  • Major comparison between a list and a Tuple
  • Lists in lists
  • Python List Methods
  • Commenting Python Code
  • How to wrap long lines in Python?

Here is the training video for Commenting Python Codes for network engineers. See and comment on this video.

 

Python Comparison Operators

Less than, greater than, equal to and not equal to for comparing the value on either side of them and decide the relation among them, they are also called Relational operators.

 

                         Operation                                 Meaning
   
                                 < Strictly less than
                                 <= Less than or equal
                                  > Strictly greater than
                                  >= Greater than or equal
                                 == Equal
                                  != Not equal
                                  is Object identify
                              is not Negated object identify

 

For example,

>>>a= “Python”

>>>a== ‘Python’

True

>>>a = 10

>>> (a==10) or (a!=5)

True

>>> (a==10) and (a!=5)

False

 

Major comparison between a list and a Tuple

Both tuples and lists are sequence data types that can store a collection of items. You can also access any item by its index and each item stored in a tuple or a list can be of any data type.

The main difference between lists and a tuples is that fact that lists are mutable (we can add delete, overwrite, existing elements) whereas tuples are immutable.

 

Lists in lists

A list is created by placing all the items (elements) inside a square bracket [], separated by commas. It can have any number of items and they may be of various types (string, float, integer, etc). Also, a list can ever have another list as an item.

>>> hostnames = [‘cisco-r1’,’cisco-r2’]

>>> type(hostnames)

<class ‘list’>

>>> IPs = [‘1.1.1.1’,’1.1.1.2’]

>>>type(IPs)

<class ‘list’>

>>>

>>>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 a single element to the end of the list.
                Python List extend () Add elements of a list to another list.
                Python List insert () Insert elements of the list
                Python List removed () Removes item from the list
                Python List index () Returns smallest index of element in list
                Python List count () Returns occurrences of element in list
                Python List pop () Removes elements at the given index
                Python List reversed () Reverses a list
                Python List sort () Sorts elements of the list
                Python List copy () Returns shallow copy of a list
                Python List clear () Clear all items from the list

 

>>>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 Python Code

Commenting your code helps explain your thought process, and helps you and other to understand later on the intension of your code. It is seen as good practice for developers and is an important part of an individual workflow. Without comments, things can get confusing, real fast.

Example-

>>> #Defining List variable

… Var_List = [“hello”, “friends”]

>>> #Defining Tuple Variable

… Var_Tuple = (“Welcome to the”, “Python”, “Course”)

>>> #Here are we are using tuples concatenation along with converting list to tuples

… Var_Tuple_addition = tuple(var_list) +Var_Tuple

>>>Var_Tuple_addition

(‘hello’, ‘friends’, ‘Welcome to the’, ‘Python’, ‘Course’)

 

How to wrap long lines in Python?

The preferred way of wrapping long lines is by using Python’s implied lines continuation inside parentheses, brackets and braces, sometimes using a backlash look better. Make sure to indent the continued line appropriately.

Examples of parenthesized line break:

>>>list (“Hello”)

[‘H’, ‘e’, ‘l’, ‘l’, ‘o’]

>>>list (

… “Hello”

… “Friends”

… )

[‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘F’, ‘r’, ‘i’, ‘e’, ‘n’, ‘d’, ‘s’]

 

Example of back-slashes line breaks:

>>>print ( “Hello Friends \

… Welcome to Python \

… World”

… )

Hello Friends Welcome to Python World

>>> a = 1 + 2 \

… + 3+ 4 \

… + 5

>>> a

15

To get instructor-led training for Python automation and programming, visit: https://www.uninets.com/python-course-for-network-engineers/