Python Scripting For Storage Admin Part 5 - If Else For Loop

Python Scripting For Storage Admin Part 5 - If Else For Loop

In this blog I have explained how we can use if else and for loop in python scripting. These two concept in python scripting is very important because once you know how to implement these in scripting you can convert any task into scripting and automate it.

Python Scripting For Storage Admin Part 5 - If Else For Loop
Python Scripting For Storage Admin Part 5 - If Else For Loop

Below three line we have created two list and and one variable. We will use these as example while using for loop and if else.

ls = ["a","b","c","d"]
ls1 = [1,2,3,4]
line = "i am a list"

In below line we have created a list from the variable "line" using split function. A space character is used within double quotes to create the list.

conv_list = line.split(" ")
print(conv_list)

Below is the output of above print function.

['i', 'am', 'a', 'list']

Now in below line we will use for loop to print all element of the list "conv_list". For loop will iterate over each element of the list and assign the value of the element to a temporary variable "i" and in next line it will print it.

for i in conv_list:
    print(i)

Output of above for loop is as below
i
am
a
list

Below is an example of if and else statement. If the specified condition under "if" statement is true then the block under "if" will get executed other wise the block under "else" will get executed.

if "c" in ls:
    print(ls)
else:
    print("Not found")

Output of above if else code is as below.

['a', 'b', 'c', 'd']

Watch below video for live demo of this tutorial. Dont forget to share and subscribe to the YouTube Channel for more videos on this series.

                    

No comments:

Post a Comment

Zoning In Cisco MDS SAN Switch In Command Line

Zoning In Cisco MDS SAN Switch In Command Line Zoning is a process of grouping initiator and target ports WWPN which is performed in SAN ...