Here are some course notes for getting started with Python. In this article, we’ll look at python functions.
Want to use the same piece of code several times, without having to rewrite it each time? This is where “functions” come in!
Here’s a simple example:
First, we define a function called
sayHello. The function takes in a variable called
name and uses it to print out a greeting.
We then use the function by calling it with the values we want to give it within parenthesis.
We could feed in several variables to a function too, like this:
Functions can also return values too. For example:
We’re going to use functions later…
Mortgage Interest - Is It Interesting?
If you’ve bought a house, or are thinking about it, you’ve probably spent more time than you wanted, thinking about mortgage payments.
Calculating mortgage payments isn’t something I personally like doing, so it would be nice if it was easy. wikiHow has a pretty good description of the maths, but I wouldn’t want to go through the calculation every time. Perhaps some Python could help.
Below is that calculation in Python, calculating a 25 year mortgage on $200,000 at 5%.
First we import the
math library, which we need for
math.pow later, then we set some values. We then convert these to “floating point” values. These are numbers that may have a decimal point in them, such as 12.99. There could be many, few, or no digits after the decimal point, hence the term “floating”.
Next, we do the calculation. No need to worry about this too much, but if you want to, feel free to look at the equation on the wikiHow page, and compare it to the Python code. Don’t be put off if you need to write it down and take some time over it. It took me a while too!
Last, we just print out the answer from the calculation, which is the amount of monthly payments required to pay off the mortgage.
Now The Tricky Part
Can you take the mortgage calculation code and turn it into a function, and use it in some code? It would need to accept values for
principle. What value would it return?
Don’t forget to
import math too. Imports should always go at the top of your code.