Home > Python, Routing > Learning Python: Week3 (Conditionals and For Loops) -Part 1

Learning Python: Week3 (Conditionals and For Loops) -Part 1

As discussed in post  ( https://crazyrouters.wordpress.com/2017/02/25/learning-python-kirk-byers-python-course/  ) , i will be sharing the my learning on weekly basis as course continues. This will not only motivate me but also help others who are in phase of learning python 3.

This post will focus on Week 3 (Conditionals and For Loops)  Exercises. As Usual , lets start with notes and will focused on exercise in next post.

If Else Condition:

How can you say that any condition is true or false ? Lets first understand different scenarios with examples.

Empty String (‘  ‘)       >>>> False  , any other string case       >>>> True

0  and 0.0                    >>>>> False , all other number value >>>>>True

Blank list [ ]               >>> False , all other list value              >>>>> True

Blank Tuple()            >>>False , all other tuple value                 >>>True

Blank Dictionary {} >>> False , all other dictionary  value    >>> True

Few more example:

1

IF Else Syntax:

if expression 1:
statement
statement
statement

elif expression 2:
statement
statement
statement

else expression 3:
statement
statement
statement

Outside of if else condition

If expression 1 is true , process the statement below it and come out of the if else loop , if expression 1 is false , check for condition for expression 2 , if true ,process the statement below and come out of if else loop, otherwise check for condition for expression 3 and if true ,process the statement below it otherwise come out of if else loop and process .

Its better to avoid the nested loop , if loop goes beyond level 3. Try to replace the nested loop with more simple code .

2.PNG

Above code can be replaced with more simple and convenient code as below.

3.PNG

FOR LOOP :

Its iterate over the items of any sequence as a list or string

Code

for1

We have  list  Vendors  storing names of vendors as string , so we want to iterate over the items of list and print it.

we can take any other variable in place of vendor too  in above code .

output

for_out

we can also have the above code as below:

for2.png

This will give the same output as before

for index in range(len(vendors)):

>>> vendors = [‘cisco’,’juniper’,’alcatel’,’hp’,’adva’]
>>> len(vendors)
5
>>>
>>> range(5)
range(0, 5)

so it  iterate over the list  ‘vendors’  having total  5 elements and and print vendors element as output for each index

iteration 1 , index =0  , vendors[0]

iteration 2 , index =1  , vendors[1]

iteration 3 , index =2  , vendors[3]

iteration 4 , index =3  , vendors[4]

iteration 5 , index =4  , vendors[5]

But there is much better way to have the above codes using enumerate ,where it takes care of index as well as value .

for3.png

It will result in same output as before , but its better way to write the code.

for3_out.PNG

Nested For loop:

Lets start with  simple example  which explains the nested for loop.

for4

a and b is list having element as numbers , as below

>>> a =list(range(3))
>>> a
[0, 1, 2]
>>> a=list(range(4))
>>> b
[0, 1, 2, 3]
>>>

So each iteration of outer loop, it should go via entire inner loop till condition is satisfied, in above loop as condition  j==2 is true, its comes out of inner loop , and outer loop again starts with index value incremented by 1 ,this this process goes on as below

i = 0 , j=0 ,condition = false , print(“hello world”)
i = 0 , j=1,condition = false , print(“hello world”)
i = 0 , j=2 ,condition = true , Come out of inner loop
i = 1 , j=0 ,condition = false , print(“hello world”)
i = 1 , j=1 ,condition = false , print(“hello world”)
i = 1 , j=2 ,condition = false , Come out of inner loop
i = 2 , j=0 ,condition = false , print(“hello world”)
i = 2 , j=1 ,condition = false , print(“hello world”)
i = 2 , j=2 ,condition = false , Come out of inner loop

output:

for4_out

Note that you cannot have a for or if else loop without any statement , otherwise will get an  error !!

for i in a:

 

or

if True:

 

There are the cases where we don’t want any statement in for or if else , we can have the same by using the ‘Pass’

for i in a:

pass

or

if True:

pass

Passing argument into script:

We can pass the argument into the script.Lets start with an simple script example

CODE:scriptarg

Here we have import sys and printed sys.argv , it will print the scriptname and all the argument you entered while running this script .

OUTPUT:

scriptargout.PNG

If we want to limit the number of argument to be entered ,this can be done too as per the code below:

CODE:

scriptarg1

Here limit the argument to 2 , if more than 2 argument entered .It will show the Error

OUTPUT:

scriptarg1_out.PNG

Its been a long post , but its worth to go once through It. I will be back with EXERCISES in next post ,So be ready to crack the Exercise,.Smiles 🙂

Advertisements
Categories: Python, Routing Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: