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 concepts in python scripting are very important because these are commonly used concepts.

If-Else and For Loop are conditional statements. You can If-Else to check a certain condition and based on the result you can perform certain tasks.

In case of for loop, you can use to perform certain same task multiple times. In later section, we will see how to use them in our script.

Once you know how to implement these in scripting you can convert any task into scripting and automate it.

If Else and For Loop In Python Scripting For Storage Admin
If Else and For Loop In Python Scripting For Storage Admin

You can check out the first post on this series where we have explained why should you learn python scripting.

If Else and For Loop In Python Video

If Else and For Loop in python are common in use. Hence we have a video tutorial as well that explains it in a better way. Don’t forget to share and subscribe to the YouTube Channel for more videos on this series.

Examples of List In Python

Below are a few examples of lists in Python. Fist list ls, stores only string elements while the 2nd list contains integer values. Thrid line is just a variable that stores a value “I am a list”

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

Create a Python List Using Split Function

We can create a list just by assigning, multiple values to a variable. You can also use the split function to create a list. In the coming section, We will use this method often in our script.

Below two lines of code create a list from the variable “line” using the split function. You must specify a separator to create the elements of the list. In the below example space is the separator.

conv_list = line.split(” “)
print(conv_list)

Below is the output of the above code.

[‘i’, ‘am’, ‘a’, ‘list’]

Examples For Loop in Python List

For loop is a conditional statement and very common in scripting. Hence, you must practice and become an expert.

Now in the below line, we will use “for loop” to print all elements of the listconv_list“. For loop will iterate over each element of the list and assign the value of the element to a temporary variable “i“. After that in the next line, it will print the value of “i“.

for i in conv_list:
    print(i)

The output of above for loop is as below

i
am
a
list

Examples Of IF Else In List

Below is an example of if and else statement. If Else, statement of python consists of two sections. If a specified condition under “if” statement is true then the block under “if” will get executed, otherwise the block under “else” will get executed.

In the below examples we checking a condition that if the string “c” exists in list “ls”. If it exists then the code under if block gets executed and print values of the list.

if “c” in ls:
    print(ls)
else:
    print(“Not found”)

The output of the above if-else code is as below.

[‘a’, ‘b’, ‘c’, ‘d’]

Leave a Comment