Posts

45 filter() method in python

  ''' 1. The filter() method filters the given sequence with the help of a function and  retun filter object. 2. filter taks two parameters one is function another one is iterable object ''' Use lambda function with filter() The Python built-in filter() function accepts a function and a list as an argument.  It provides an effective way to filter out all elements of the sequence.  It returns the new sequence in which the function evaluates to True. ## Filter with lambda function fo = filter(lambda a: a % 2 == 0 ,[1,2,6,4,5]) print(list(fo)) #program to filter out the tuple which contains odd numbers     lst = (10,22,37,41,100,123,29)   oddlist = tuple(filter(lambda x:(x%3 == 0),lst)) # the tuple contains all the items of the tuple for which the lambda function evaluates to true     print(oddlist)  

44 map function in python

  ''' 1. map is used to do operation on each iterable value and  it will return all operations result in map object foramt. 2. Map takes two parameters one is function  another one is iterable object ''' ## Map with def fucntion def add(a):     return  a + 10 #mo = map(add, (1,2,3,4,5,6)) #print(list(mo)) ## Map with lambda function #mo = map(lambda a, b: a + b ,[1,2,3,4,5,6], [11,22,33,44]) #print(tuple(mo))

43 Lambda function in Python

  ''' 1. Lambda is a another way to create a function. 2. Lambda is a one line anonymous function it doesn't contains name. 3. Using lambda we can do only one expression. ''' ## Lambda with out arguments #add = lambda : 3 + 5 #r = add() #print(r) ## Lambda with arguments  #add = lambda a,b: a + b #r = add(2, 33) #print(r) ## Lambda with if and else add = lambda a,b: a + 10 if a > 5 else b + 100 print(add(6,6)) ------lambda with Map----- # Double the value of each element myList = [10, 25, 17, 9, 30, -5] myList2 = map(lambda n : n*2, myList) print myList2 ------lambda with filter------ # Filters the elements which are even numbers myList = [10, 25, 17, 9, 30, -5] myList2 = list(filter(lambda n : (n%2 == 0), myList)) print(myList2) ------Use lambda function with filter()----- The Python built-in filter() function accepts a function and a list as an argument.  It provides an effective way to filter out all elements of the sequence.  It returns the new sequen...

42 standard input in python

 ''' 1. Using standard input we can get values at run time. 2. using input() function we can get run time values in python. ''' uname = input('\n Please enter your name ::') print(' \n UNAME IS : ', uname) #========== pin   = input('\n Please enter PIN   ::') print(' \n PIN   IS : ', pin)

41 Encapsulation in python

  ''' 1. Encapsulation is the process of wrapping up variables and methods into  a single entity. In programming, a class is an example  that wraps all the variables and methods defined inside it. class One():     name = 'umamahesh'     def add(self):         print(' ADD is ')     def sub(self):         print(' SUB is:') ins = One() print('\n ', ins.name) --Using encapsulation we can give protection to the class members.  It can achieve Using access modifiers. Encapsulation can be achieved using Private and Protected Access Members. ''' Example---CSE department faculty cannot access the ECE department student record and  so on. Here, the department acts as the class and  student records act like variables and methods. =======Why Do We Need Encapsulation?=========== ===Encapsulation acts as a protective layer by ensuring that,  access to wrapped data is not possible by any code define...

40 Access modifiers in python

 ''' 1. All name variables and methods are public by default in Python 2. Protected variable we can define By prefixing the name of your variable or  method with a single underscore.  Protected mean you’re telling others “don’t touch this, unless you’re a subclass” 3. private variable we can define By prefixing  the name of your variable with a double underscore, By declaring your data member private you mean,  that nobody should be able to access it from outside the class. ''' class One():     name   = ' public '     # public     _name  = ' protect '    # protect     __name = ' Pravate '    # pravate     def sum(self):         #self.__sub()         print(' \n Public ' )     def _add(self):         print(' \n Protect ' )     def __sub(self):         print(' \n Pravate ') ins ...

39 abstract method in python

  ''' 1. An abstract method is a method that is declared, but contains no implementation. 2. By defining an abstract base class,  you can define a common API for a set of subclasses. 3. By using abc module we can define abstract class. ''' from abc import ABCMeta,abstractmethod class One(metaclass=ABCMeta):     @abstractmethod     def add(self):         print(' \n ONE ADD ')     @abstractmethod     def sub(self):         pass #a = One() #a.add() ## If you inheritence an abstaruact class  you should impliment that all those methods which are abstract ,methods class Two(One):     def add(self):         print(" \n TWO ADD")     def sub(self):         print(' \n TWO SUB') ins = Two() ins.add() ins.sub()