Learning objectives
By the end of this section you should be able to
- Write a module that consists only of function definitions.
- Import the module and use the functions in a program.
Defining a module
Modules are defined by putting code in a .py file. The area
module below is in a file named area.py. This module provides functions for calculating area.
Example 7.1
The area module
"""Functions to calculate the area of geometric shapes."""
import math
# 2D shapes
def square(side):
"""Gets the area of a square."""
return side**2
def rectangle(length, width):
"""Gets the area of a rectangle."""
return length * width
def triangle(base, height):
"""Gets the area of a triangle."""
return 0.5 * base * height
def trapezoid(base1, base2, height):
"""Gets the area of a trapezoid."""
return 0.5 * (base1 + base2) * height
def circle(radius):
"""Gets the area of a circle."""
return math.pi * radius**2
def ellipse(major, minor):
"""Gets the area of an ellipse."""
return math.pi * major * minor
# 3D shapes
def cube(side):
"""Gets the surface area of a cube."""
return 6 * side**2
def cylinder(radius, height):
"""Gets the surface area of a cylinder."""
return 2 * math.pi * radius * (radius + height)
def cone(radius, height):
"""Gets the surface area of a cone."""
return math.pi * radius * (radius + math.hypot(height, radius))
def sphere(radius):
"""Gets the surface area of a sphere."""
return 4 * math.pi * radius**2
Concepts in Practice
Defining a module
Importing a module
The module defined in area.py can be used in other programs. When importing the area
module, the suffix .py is removed:
import area
print("Area of a basketball court:", area.rectangle(94, 50))
print("Area of a circus ring:", area.circle(21))
The output is:
Area of a basketball court: 4700 Area of a circus ring: 1385.4423602330987
Concepts in Practice
Importing a module
Try It
Conversion module
Write a module that defines the following functions:
cel2fah(c)
–
Converts a temperature in Celsius to Fahrenheit.
The formula is9/5 * c + 32
.fah2cel(f)
–
Converts a temperature in Fahrenheit to Celsius.
The formula is5/9 * (f - 32)
.km2mi(km)
–
Converts a distance in kilometers to miles.
The formula iskm / 1.60934
.mi2km(mi)
–
Converts a distance in miles to kilometers.
The formula ismi * 1.60934
.
Each function should include a docstring as the first line. A docstring for the module has been provided for you.
The module should not do anything except define functions. When you click the "Run" button, the module should run without error. No output should be displayed.
Try It
European vacation
Write a program that uses the conversion
module from the previous exercise to complete a short story. The program's output should match the following example (input in bold):
How fast were you driving? 180 Woah, that's like 112 mph! What was the temperature? 35 That's 95 degrees Fahrenheit!
Notice this exercise requires two files:
- european.py, the main program. Input and output statements are provided as a starting point. Edit the lines with TODO comments to use the conversion module.
- conversion.py, the other module. Copy and paste your code from the previous exercise. Import this module in european.py after the docstring.