USD ($)
$
United States Dollar
India Rupee

Learn Fundamentals of Python

Created by Vanshika Sharma in Articles 16 Sep 2024
Share
«Basics of Ansible: Playbooks and ...

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?

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)

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

Understanding Ansible YAML Basics»
Vanshika Sharma

More... | Author`s Bog | Book a Meeting

Related Articles

#Explore latest news and articles

Why is Network Automation Important? 12 Nov 2024

Why is Network Automation Important?

Discover the crucial role of network automation for network engineers. Learn why automation is essential for efficiency and competition in the networking field.
Understanding Ansible YAML Basics 29 Oct 2024

Understanding Ansible YAML Basics

Learn the fundamentals of YAML with a focus on basic syntax. This YAML basics tutorial is perfect for beginners, providing a step-by-step guide to YAML.
Learn Python from Scratch: Begin Here 16 Nov 2024

Learn Python from Scratch: Begin Here

Explore Python with our guide on learning Python from scratch. Master the fundamentals and elevate your programming skills step by step.

Comments (0)

Share

Share this post with others

Contact learning advisor

Captcha image