Advanced topic
List comprehension:
We have seen power of python programming, explaining one more powerful feature of Python. List comprehension provides a compact way of mapping a list to another list by applying user defined logic.
1 2 3 4 |
my_list = [1,2,3,4,5,6,7] new_list = [each for each in my_list if each%2] print new_list [1, 3, 5, 7] |
In the above example:
- we have create a list [1,2,3,4,5,6,7]
- using list comprehension we have created new list of odd integers
- Note, list comprehension doesn’t change the original list
- Python constructs the new list in memory, and when the list comprehension is complete, it assigns the result to the variable
Lambda function:
Python provides very interesting one liner function which doesn’t have any name, called Lambda function.
1 2 3 4 5 6 |
g = lambda x: x*2 print g(3) 6 f = lambda x, y : x * y print f(2,3) 6 |
- Syntax of Lambda function is “lambda argument_list: expression”
- multiple argument should be passed using comma separated
- It doesn’t have return keyword
Filter function : This function take first argument as function and second argument will be sequence. Sequence will use the function and only those values will be captured which return True.
1 2 3 4 |
my_list = [0,1,2,3,4,5,6,7,8,9] odd_list = filter(lambda x:x%2, my_list) print odd_list [1, 3, 5, 7, 9] |
Map function : Map function takes two arguments, first argument is function and second one is sequence . you can pass single or multiple sequence.
1 2 3 4 5 6 7 |
my_list = map(lambda x,y:x*y,(1,2,3),(10,20,30)) print my_list [10, 40, 90] my_list = map(lambda x:x*2,(1,2,3)) print my_list [2, 4, 6] |
Reduce function: Reduce function is different than map and filter, you must import reduce from the functools module explicitly. function reduce(f, seq) continually applies the function f() to the sequence seq and returns a single value.
1 2 3 4 5 6 7 8 |
from functools import reduce my_list = reduce(lambda x,y:x+y,(1,2,3)) print my_list 6 my_list = reduce(lambda x,y:x*y,(2,3,4)) print my_list 24 |
Generator : In simple word,a generator looks like a function but behaves like an iterator. It has a yield keyword. a generator yields the values one at a time, which requires less memory. Lets generate Fibonacci series using generator function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def fibonacci(): """Fibonacci numbers generator""" a, b = 0, 1 counter = 5 index = 0 while index <= counter: index = index +1 yield a a, b = b, a + b for entry in fibonacci(): print entry ##### Output of the code ######### 0 1 1 2 3 5 |
Note :you can have multiple yield keyword in single function.
Decorator : Python decorator are wrapper of a function. They executed first then calling function get executed.
Let me take a small example to explain decorator, I need to print entry and exit time of function.
- decorator denoted with @ symbol
- first decorator function get called then main function get called
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import datetime def my_decorator(func): def wrapper_fun(): print "entry time:: %s" % datetime.datetime.now() s = func() print "exit time:: %s" % datetime.datetime.now() return wrapper_fun @my_decorator def fun(): print "Hi" fun() ########## Output of function entry time:: 2015-10-20 23:59:31.794000 Hi exit time:: 2015-10-20 23:59:31.810000 ######## Decorator example with arguments ################## def my_decorator(func): def wrapper_fun(*args, **kwargs): #1 print "Arguments were: %s, %s" % (args, kwargs) s = func(*args, **kwargs) #2 print "ending Arguments were: %s, %s" % (args, kwargs) return wrapper_fun @my_decorator def fun(*args,**kargs): print "Hi" fun(1,2,c="Hello") ########## Output of function Arguments were: (1, 2), {'c': 'Hello'} Hi Exiting from decorator function |