Saturday, May 26, 2018

python chap 10 (Hi friends this is the continuation of previous post so i kindly request you to read that before this post. As this blog is also prepared for beginners so i advice you that you don't need to by-heart this lessons just read and understand it if you have any doubt ask in the comment section)

Control Flow Tools

The control flow is the way to control the code to process or control flow is the order in which the code executes simply by using control flow tools we control which code should execute and how many times a code should execute like stuffs more will be understood from the section given below

if Statements

Perhaps one of the popular statement is the if statement as we can see an example here

x = 12
if x==10:
     print('x is 10')
elif x == 21:
        print ('x is 21'):
elif x== 22:
       print('x is 22')
else:
       print ('x is 12')
#here the result will be 'x is 12'
here the statement checks if x is equal to 10 but it is already assigned 12 and then the code jumps to elif statement and checks the elif  is used to check another statement as if x is equal to 21 and it is also not equal to then it checks other elif and after that at last to finish if every thing returns false we use else as last resort to put a default value there we print as x is 12 then it will be the result here
Now we can make a nested if else statement the examples are given below
x = 12
if x < 15:
     if x==13:
         print('x is 13')
      else:
           print (x is 12)
if x==12:print('x is 12')
          print('hi everybody')
here we have learned that if statement without else and nested if statement is also made here
and if the suite of an if clause consists of single line then it may go on the same line as a statement like above statement where the out put will be x is 12 and in the next line hi everybody


while statement

The statements are executed sequentially one after the other there may be a situation when we need to execute a block of code several number of times here comes the loop statement which helps us in executing a code block several number of times one of that is the while statement 
now we can see it in the code here given below simply while statement is used for the repeated execution of code as long as the given expression is true  
x = 10
while x < 10:
        print (x)
        x = x + 1
5
6
7
8
9 #the output will be as follows till the condition in the while statement x<10 is true
In here we can see that what the while statement will do in here the statement puts a condition as x<10 so here the code below which is print (x) will print the value of x and after that the x is added  with 1 and until the condition become false x < 10 the print statement will print the value of x it will result in the out put of 5,6,7,8,9 

for loop statement

The for loop statement is somewhat different from other programming languages where the for loop statement in here the statement in the for loop is executed until the for statement iterates over the item of any sequence (a list of a string ),in the order that they appear in the sequence is complete as for example :

my_list = ['dog','god','direction','hit','yeild']
for letters in my_list:
       print (letters,len(letters))

dog 3
god 3
direction 9
hit 3
yeild 5
In here we can see that the values in my_list is printed one by one with its length here the process is that the items in the list my_list is put one by one in the letters and printed until all the items are printed and there is another way of using for loop is that the range() function here is an example
for letter in range(5):
              print(letter)

0
1
2
3
4
In here the values in the range from 0 to 5 is put one by one in letter and printed out more on range will be given in the next post and in for loop we can put the sliced sequence in the for loop to execute statement as example


my_list = ['man','jack','ace','mike','jill','cap','care']
for words in my_list[2:5]:
           print(words)

ace
mike
jill

We can see here that the items in the my_list from 2 to 5 is only printed by putting the sliced list in the for loop

Thats it friends more on this lesson like range() will be explained in the next post till then understand the stuff given here and if you have any doubts or suggestions please comment below your valuable suggestions can improve this blog further more and don't forget to share and if you want to get latest lessons please follow by email or follow me

No comments:

Post a Comment