6 – More maths

Moving on from the simple add, minus, multiply and subtract there are many other maths functions you may need in the future.

Before we start we need to import something. Importing is normally done on the first few lines of code. Importing is when we get a module and we allow our selves to use the functions from it. We do this because there are many functions in a programming language, and it would be unnecessary to have them all there for us to use at all times when we will not use most of them. The module we will be importing is called math. To import it you can type import math. However we will not import it this way, this is because if it is imported this way, every time a function from this module is used we must type (math.) before it, which is annoying, and we aren’t importing many modules so it isn’t necessary.

The way we will import it is by saying

from math import *From math

Where the star is you could type the function name and it will only import that function but the star means that every function in this module is being imported.

There are many functions in this module, most of which I will not show, I recommend reading up on them here.

https://docs.python.org/2/library/math.html

Before we start using what we got from the math module we should use the power of symbols. Often when doing maths you will need to square a number or cube it, etc, to do this in python you must use a double multiply sign followed by the number you wish to put it to the power to.

8 ** 28 squared.

This will return 64.

As well as squaring numbers you may wish to square root it, to do this we us a function from the math module called sqrt(). Within the parentheses you type the number you wish to square root.

sqrt(64)sqrt 64

This will return 8.0, remember to put it in a print function to print the answer to the console. It will return a float, meaning a decimal number, so instead of just 8 it will say 8.0. To turn a float into an integer you should put the sqrt function in the parentheses of int() (int can be used without the math module). This can all be done on one line.

print(int(sqrt(64)))print int

This will return 8.

The math module also allows us to round numbers up and down using the ceil() and floor() functions. These work by putting a decimal number into the parentheses and it will round them up or down. Lets round 15.7. To round this number up we use ceil(15.7), to round it down we use floor(15.7).

ceil floor

They both will return an integer, ceil(15.7) will return 16, floor(15.7) will return 15.

Finally, something that may come in handy is using pi. Pi is a variable from the math module that equals the number pie, this allows us to work with equations relating to circles or with what ever you wish. You can use it the same as any other variable.

4 * pipi.

This will return 12.566370614359172, maybe this will come in handy for you.

I recommend taking a look at the other functions available in the math module.

https://docs.python.org/2/library/math.html

If you struggle understanding some things about the programming maybe my other posts will help you.

https://programmingandgames.wordpress.com/category/beginner-programming/

Leave a comment