Practical programming for the normal person: Modules

Practical programming for the normal person: Modules

(Syda Productions/Shutterstock)


Save Story
Leer en español

Estimated read time: 5-6 minutes

This archived news story is available only for your personal, non-commercial use. Information in the story may be outdated or superseded by additional information. Reading or replaying the story in its archived form does not constitute a republication of the story.

SALT LAKE CITY — This week we need to discuss modules. [Last week I wrote a mortgage calculator function](http://www.ksl.com/index.php?sid=34579853&nid=1012&title=practical-programming-for-the-normal-person-functions), but as soon as we closed the Python interpreter, our function disappeared.

If we want to go back and check the monthly payments for a different house, we will have to type that whole function in again. We can fix this by putting it in a file and using the file as a module. Python also has plenty of its own built-in modules. Anyone who needs to use Python for more than basic arithmetic can benefit from modules.

Modules are merely files that store code. They can hold variables, functions, or both. To be used, modules must be loaded each time Python is started, but this is easier than typing out your functions every time. Let's start with a built-in module.

For the normal person, there is one module that may be especially handy. It is called math. To load it, run import math. This module contains many commonly used math functions, as well as a few constants. Constants are values that never change, like pi. To find the area of a circle with a 2 unit radius using pi from the math module, you would run math.pi * 2**2 this after importing the math module.

The math module contains many commonly used trig functions, such as sin, cos, tan, asin, and so on. These all take arguments in radians, not degrees. It also has functions for converting between radians and degrees. If you wanted to find the sine of a 30 degree angle, you would run math.sin(math.radians(30)). Note that the answer comes out as 0.4999... instead of 0.5. This is because floating point math is not 100 percent accurate, due to limitations of digital math. In this case, we can round this to 0.5. We can convert back to degrees by running math.degrees(math.asin(0.5)). See Python's documentation for more functions in the math module. Python can also support complex numbers with the cmath module.

Related:

We will conclude this article by putting our mortgage payment function into a module file. For this, you will need to have at least a basic understanding of the file system and be able to navigate to a specific directory or folder from a command line.

First, choose where you want your Python modules stored. If you want to keep things organized, consider creating a new directory for them. To use your modules, you will need to run Python from the directory where they are stored. This will require navigating to that directory from a command line, then running Python from there.

Once you know where you want your module, open a text editor. You will need something like Notepad or TextEdit, not a word processor (the author prefers Notepad++ when using Windows). Type in the mortgage calculator function from last week.

def mort(interest, payments, principal):<br></br>&nbsp&nbsp&nbsp&nbspr = interest / 100. / 12&nbsp&nbsp&nbsp&nbspc = (r * principal) / (1 - (1 + r)**-payments)&nbsp&nbsp&nbsp&nbspreturn c

Now save this in the directory you have chosen for storing your modules. Choose a simple name for the module, that you can remember .loan would be a good game, then add .py to the end of the name. In Notepad, you will need to put quotes around the name, or it will add .txt to the end, which will confuse Python. Assuming you want to name your module loan, you would save it in Notepad as "loan.py". In Notepad++ and most other editors, you do not need the quotes.

Now, open a command line, navigate to the directory where you saved the file, and run Python. To load your new module, run import loan. Now you can find the monthly payments on a $100,000, 15 year loan with a 3.0% APR by running loan.mort(3.0, 15 * 12, 100000).

You can add more functions to your loan module. A function for determining the total cost you will pay for a given loan would be really handy. You could add the following function to the file below the mortgage calculator.

def cost(interest, payments, principal):&nbsp&nbsp&nbsp&nbspmonthly = mort(interest, payments, principal)&nbsp&nbsp&nbsp&nbspreturn monthly * payments

Because this is going inside the module, you can use the mort() function directly, without the loan. before it. Once you save this in your loan.py file, you can import the module and run loan.cost(3.0, 15 * 12, 100000) to find the total cost of the house if you only make the minimum monthly payments. If you have already loaded the module, and you change the file without restarting Python, you can run reload(loan) to load the new version of the module.

This is the end of Practical Programming for the Normal Person. Python is a very commonly used programming language. If you need to do something beyond what you have learned here, a simple web search is sure to turn up a lot of help. If you cannot find help for your particular problem, there are plenty of forums where friendly people are willing to answer almost any question.

Click to read parts one, two and three of the Practical Programming series on KSL.com.

Related links

Related stories

Most recent Features stories

STAY IN THE KNOW

Get informative articles and interesting stories delivered to your inbox weekly. Subscribe to the KSL.com Trending 5.
By subscribing, you acknowledge and agree to KSL.com's Terms of Use and Privacy Policy.

KSL Weather Forecast