Page 252 - Computer Science V2.0 Class 11
P. 252
>>> applyFn(math.sqrt, 16)
4.0
>>> applyFn(math.sin, math.pi/2)
1.0
9.3 User-defined Modules
A Python module is nothing but a file comprising Python code. It can be imported into another program, just like
other modules available in Python. In this section, we will build a module geometry.py that enables computation
of the area and perimeter of rectangles and triangles. We encourage you to extend this module by incorporating
the definitions of the functions for computing the area and perimeter of some more geometric figures. Currently,
the module comprises the definition of the functions to compute the area and perimeter of rectangles and triangles
in the file geometry.py. As the computations are self-explanatory, we do not describe these functions further in
the text.
1. #Function to Compute Area of a Rectangle
def areaRectangle(length, breadth):
'''
Objective: To compute the area of rectangle
Input Parameters: length, breadth – numeric value
Return Value: area - numeric value
'''
area = length * breadth
return area
2. #Function to Compute Perimeter of a Rectangle
def perimeterRectangle(length, breadth):
'''
Objective: To compute the perimeter of rectangle
Input Parameters: length, breadth – numeric value
Return Value: perimeter - numeric value
'''
perimeter = (length + breadth) * 2
return perimeter
3. #Function to Compute Perimeter of a triangle
def perimeterTriangle(side1, side2, side3):
'''
Objective: To compute the perimeter of a triangle
Input Parameters: side1, side2, side3: – numeric values
Return Value: perimeter - numeric value
'''
perimeter = side1 + side2 + side3
return perimeter
4. #Function to Compute area of a triangle
def areaTriangle(base, height):
'''
Objective: To compute the perimeter of a triangle
Input Parameters: base, height: numeric values
Return Value: area of triangle: numeric value
'''
#Approach: Formula used: area = height*base/2
area = height*base/2
return area
238 Touchpad Computer Science (Ver. 2.0)-XI

