Posts

11 conditional operations in python

 1. Using if, elif and else we can perform conditional operations 2. Using conditioanl operations we can execute a block of code based on condition # Below are the python operators to perform conditional operations     1. Arthmetic operators(+,-,*,%,/,//)         1. Uesd to do addition and substraction operations     2. Assignment operators(=,+=,-=,*=,%=,/+,//+)         1. Used to assign value to variable     3. Comparison operators(==,!=,>,>=,<,<=)         1. These operators compare the values on either sides of them and decide the relation among them.              They are also called Relational operators.     4. Logical operators(and,or,not)         1. Using logical operations we can execute a block of code based on multiple condition.     5. Membership operators(in,not in)         1. Using member ship operations we can find a value in list and sub string in a string     6. Identity operators(is,is not)         1. Using identity operators we can check memory location of

12 Loop statements in python

 1. Using loops we can execute a block of code repeatedly 2. Python support 3 types of loops     1. For loop    ==> for loop Iterate based on range     2. While loop  ==> while loop Iterate based on condition     3. nested loop ==> If a loop contains another loop that is called nested loop 3. Loop statements      1. break     ==> break used to terminates the loop     2. continue  ==> continue used to skip the current iteration      3. pass      ==>          1. pass will do nothing          2. we use pass when statement is require to aviod syntax issues. 1) for loop with range ''' 1. Range() function used to craete a range of values ''' #for n in range(8):        # 0 to n-1  #  print(n) #for n in range(3, 8):     # n to n-1  #   print(n) #for n in range(8, 15, 3):  # n to n-1 , step #    print(n) for n in range(5, 8):     print(n)  #for i in range(4, 8): #    print(i) #    if i > 4 : break #for i in range(4):     # if i : continue     # print

13 command line parameters in python

 ''' 1. You can access to the command line parameters using the sys.argv   Example ::- command_line_argu.py "uma mahesh" 78 ''' import sys ## To get first command line argument print(' argv[1] :: ' , sys.argv[1]) ## To get second command line argument print(' argv[2] :: ' , sys.argv[2]) ## To get file name with full path #print(' argv[0] :: ' , sys.argv[0]) ## To get all command line argument with file name #print(' argv :: ' , sys.argv)

49 decorators in python

 ''' 1. used to modify the behaviour of function or class. Using decorators we can change a behaiviar of function  without changing any fucntion code. 2. using @ we can assign decorator to the function ## How to create decorator Decorator fucntion takes one argument as function Decorator function contains another wrapper function Inside wrapper function only we have to call function argument Decorator function will return wrapper function # Decorator are used to create the static method class One():     @staticmethod     def add():         print(' \n Static Method') #ins = One() #ins.add() One.add() #Using @classmethod decorator we can create Class method. class One():     name = 'umamahesh'     @classmethod     def sub(cls, n):         print(' \n id of One : ', id(One))         print(' \n id of cls : ', id(cls))         cls.name = n         print(' \n class variable is :: ', One.name) #ins = One() #ins.sub('kumar') One.sub(&#

48 generator in python

  ''' 1. Using generator we can execute a block of code  based on demand by using next() function. 2. using yield keyword we can create a generator. ''' def gen_fun(n):     for i in range(n):         print('Before yield')         yield i         print('After yield') gen_obj = gen_fun(10) #print(next(gen_obj)) #print(next(gen_obj)) # print(next(gen_obj)) ## Iterate based on for loop #for i in gen_obj: #    print(' For Loop ', i) def gen_fun(n):         print('Before return')         yield n         print(' \n After return')         yield n gen_obj = gen_fun(10) print(next(gen_obj)) print(next(gen_obj))

47 iterator in python

 ''' 1. using iterators we can get sequence of values  based on demand by using next() function. 2. We can create iterators by using iter() function. ''' l = [1,2,3,4,5] #for i in l: #    print(i) #iterator creation l = [1,2,3,4,5] iter_obj = iter(l) print(next(iter_obj)) print(next(iter_obj)) for i in iter_obj:     print(' For loop ', i) #print(next(iter_obj)) # # #

14 List comprehensions in python

  ''' List comprehensions are used for creating new list from another iterables. Using list comprehensions we can do loop operations inside a square brackets. ''' l = [1, 2, 3, 4] ## Basic list comprehension #r = [ n + 10 for n in l] #print(r) ## list comprehension with if condition #r = [n + 2 for n in l if n > 2] #print(r) ## list comprehension with if and else condition r = [n + 100 if n > 2 else n + 10 for n in l] print(r)