A Quick Start Introduction to Python Programming - 2

Wednesday, September 10, 2008

In the previous introduction to python programming, we saw some basic datatypes in python, functions, conditional statements, importing modules, using functions like dir, accessing doc strings of modules/functions, etc. Again to reiterate its important to note that "Everything in python is an object", I don't mind saying this again, but when it comes to python world, this understanding is very important.

In this section we will see the differences between python and C modules and some special methods like lambda, filter and map functions in detail. If you are new to python programming, I recommend you to visit "A Quickstart Introduction to Python Programming - 1" before continuing this section.

4. Python and C (built in) modules
Although this is a simple concept, its worth understanding the differences between these two. The standard python modules may be of two different types, built in modules implemented in C or modules implemented in python. To see the difference, look at the following code.

Listing 4: modules.py



#
# Differences between Python and C modules
# modules.py
# Author: S.Prasanna
#

import sys, os
print "Details about Built in mudules and python modules"
print "Module sys type =", sys   # Built in module, implemented in C
print "Module os type =", os    # module implemented in python
1
2
3
4
5
6
7
8
9
10
11

Output:IDLE 1.2.2 ==== No Subprocess ====
>>>
Details about Built in mudules and python modules
Module sys type = 
Module os type = 
>>>
As one can see, module sys is a built-in module or in other words implemented in C and module os is a python module. One can also access C functions through python, for example python language doesn't have support for handling core system functions like managing device drivers or other important operating system routines, etc, in such a case one can use a framework like SWIG to interface python with C language and use those low level functionalities directly through python modules.

5. Special methods
Here we will discuss some special methods in python which will be convenient to handle repititive tasks.

Listing 5: special_methods.py



#
# Use of Lambda functions and special methods
# special_methods.py
# Author: S.Prasanna
#

# Example of lambda function
func = lambda x: x * x
print "%s square = %s" % (4, func(4))
print "%s square = %s" % (5, func(5))

def product(y): return lambda x: x * y

square4 = product(4)
square5 = product(5)

print "square4 =", square4
print "square5 =", square5

print "Product of 4 * 4 and 5 * 5 =", square4(4) * square5(5)
print "Product of 4 and 5 =", product(4)(5)

def filter_func (args):
""" A function which returns the remainder when a number is divided by 2 """
return args % 2

# Example of Filter function
list_input = [1,2,3,4,5,6,7,8,9,10]
list_output = filter(filter_func, list_input)
print "List contents =", list_input
print "Filter function filtered list output:" , list_output

# Example of Map function
list_input = [1,2,3,4,5,6,7,8,9,10]
list_output = map(filter_func, list_input)
print "List contents =", list_input
print "List output after map function:" , list_output
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
34
35
36
37

Explanation:

Lines 6 - 12:

Line 7 defines a lambda function like func = lambda x: x * x.

Lambda functions are something like anonymous inline functions which doesn't take take parameters similar to a normal function and don't have return values. The return value of a lambda function is the result of the expression in the lambda function. Here we define a lambda function which computes the product of a number multipled by itself, therefore func(4) will be 4 * 4 = 16 and func(5) = 25 (Lines 9 to 10).

Line 11 defines another function which inturn returns a lambda function,

def product(y): return lambda x: x * y
in other words, when we say square4 = product (4) (line 13) it is similar to this

square4 = lambda x: x * 4 (4 is the first parameter we supply to product function)
Simlarly square5 = product(5) means, square5 will have a lambda function square5 = lambda x: x * 5

Line 16 and 17 confirms that square4 and square5 are lambda functions.

Therefore square4(4) will inturn invoke the returned lambda function which will compute (and return) 4 * 4 and square5(5) will return 5 * 5, therefore square4(4) * square5(5) will be (4 * 4) * (5 * 5) = 400

Similarly product(4)(5) will be 20 because product(4) will return a lambda function lambda x: x * 4 and 5 will be used as the paramater to invoke the returned lambda function (which is anonymous), therefore computing 4 * 5.

Lines 22 - 31:

Line 22 defines a function filter_func which returns the remainder of a number when divided by 2 (divisible by 2 check).

Lines 26 - 31
# Example of Filter function
list_input = [1,2,3,4,5,6,7,8,9,10]
list_output = filter(filter_func, list_input)
print "List contents =", list_input
print "Filter function filtered list output:" , list_output
The filter function takes a function name and a list as input parameters and returns a list with the input numbers for which the filter function returns a true value (A true value in python is any integer which is > 0). Since in the above filter function, filter_func =0 for all even numbers (it computes mod 2), therefore the list will have only odd numbers [1,3,5,7,9]

Lines 32 - 37:

# Example of Map function
list_input = [1,2,3,4,5,6,7,8,9,10]
list_output = map(filter_func, list_input)
print "List contents =", list_input
print "List output after map function:" , list_output
The map function takes a function name and a list as input parameters and returns a list with the return values of the input function applied to each element in the input list.Therefore for the above list, the return value would be the remainder after the number divided by 2, which will be [1, 0, 1, 0, 1, 0, 1, 0, 1, 0].

Output:

IDLE 1.2.2 ==== No Subprocess ====
>>>
4 square = 16
5 square = 25
square4 = at 0x01ED6070>
square5 = at 0x01E72F70>
Product of 4 * 4 and 5 * 5 = 400
Product of 4 and 5 = 20
List contents = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Filter function filtered list output: [1, 3, 5, 7, 9]
List contents = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
List output after map function: [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
>>>
4 square = 16
5 square = 25
square4 = at 0x01DFB8B0>
square5 = at 0x01ED6070>
Product of 4 * 4 and 5 * 5 = 400
Product of 4 and 5 = 20
List contents = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Filter function filtered list output: [1, 3, 5, 7, 9]
List contents = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
List output after map function: [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
>>>

Continued here. A Quick Start to Introduction to Python Programming - 3.

No comments:


Copyright © 2016 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.