Sunday, June 10, 2018

python chap 11 (Hi friends this post is made as chapters so read the previous chapters for better understanding. 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)

break , continue statements  and else clause on loop

                               This statements are used as it describe break statement break out of the innermost enclosing for or while loop and the continue statement continue through the next iteration of the loop and the loop may have an else clause it is executed when the loop terminates through exhaustion of the list with for or when the condition becomes false with while loop but not when the loop is terminated by a break statement

for x in range(10,20):
        if x==15:
            print("no way for 15")
            break
        if x%2 == 0:
            print("found an even number")
            continue
        print(x," is a prime number")


for x in range(2,10):
    for l in range(5,x):
        if l == 7:
            print(x,"i got from seven to 10")
            break
    else:
       print(x,"i got from two to seven")

In this code we can clearly see the job done by each of the statements first the for loop iterates from 10 to 20 and it checks in the if statement that the iterating value in x is it equal to 15 and if it is equal then it will print "no way for 15" and break statement is done which will terminate the loop and if it is not equal to 15 then it will check in the second  if statement that x%2 is equal to zero if it is then it will print "found an even number" and then the continue statement will be done the continue statement continues with the next iteration of the loop. and if x%2 is not equal to zero then prints "x is a prime number"
And here in the next code block the first for loop iterates from 2 to 10 and each iteration from 5 there is another for loop which iterates to the value of x next to 5 and in that for loop if value is equal to 7 then it will print x"i got from seven to 10" and there is an else clause which prints from the beginning of the for loop from two to seven here one thing to note is the else clause is the else of the second for loop not the if statement

pass statement

The pass statement does nothing it is used when we need a statement syntactically but the program requires no action
while True:
          pass
This will make a busy wait for keyboard interrupt (ctrl+c) can also be used to create minimal classes like

class MyClass :
           pass


Another place where pass can be used is as a placeholder for a function or conditional body when you are working on new code allowing to thinking at a more abstract level the pass is silently ignored
def  myfunc(*args):
          pass #remember to implement this

Functions

function is a named code block of the program where a specific task is executed in python there are many built in functions like print().
  • the function starts with the keyword def 
  •  followed by a name will be given to the function the conventional style for naming of a function is like lower_case_with_underscore 
  • next comes the parenthesized list of formal parameters we can also define parameters inside this parenthesis and the code block in python starts with a colon (:) and is indented
  • in the first line of the body of the code block  can be an optional string literal this string literal is the functions documentation string or doc string  
  • And then the code is written for the specific task and the statement return [expression] exits the function optionally passing back an expression to the caller the return statement without an expression argument returns None Falling of the end of a function also returns None 

More on functions will be included in the next post Hope you like the post and if you have any doubts or suggestions include it in the comment section and don't forget to share and follow to get latest posts on this blog follow by email 

No comments:

Post a Comment