We’ve seen how Python can be used as a simple calculator. We have expressions for basic functions (multiplication, addition, etc.); but what if we wanted to do something more complex? Let’s take a look at Python’s math
library.
Setting Up “Math”
To access the math library, you’ll first have to import the library to your current programming session. Use an import statement at the top of your new document.
import math
There are many functions built in to the math library. These include things like exp()
, sqrt()
, etc. To access these, however, we must always remember to tell Python we want to use the math library. How this is done is shown below.
# Import our library
import math
math.exp(5) # Euler's number to the power of 5.
math.sqrt(4) # Square root of 4.
The math.
prefix tells Python to search the math library for that function. That way, you don’t need to write it yourself!
Complex Equations
Once you’ve got the math library imported and understand how to reference it, you’re ready to start writing equations. From here, if you’re familiar with systems like WolframAplha or MatLab, the rest will look very familiar. If not, don’t worry! It’s easy. There are just a few things you have to remember.
Parentheses
Python, like most mathematical software and text, uses parentheses to separate parts of an equation. In Python, make sure you are only using round brackets, not curly or square ones!
# Notice that
5 * (2 + 5)
# Is not the same as
5 * 2 + 5
As a rule, always use more brackets when in doubt. Too many is better than too little!
Also, it’s important to ensure that you are closing your brackets. Syntax errors in Python can be difficult to find, but IDLE will highlight text enclosed by parentheses when you type a closing bracket.
Know Your Function
Make sure you are using the functions properly. Ensure you understand what arguments the function takes, as well as what kind of value it returns (integer, float, etc.).
Here are some examples of useful functions from the library:
math.exp(x) # Returns float e^x where e is Euler's number
math.log(y) # Returns float log(y) base e where e is Euler's number
math.log(y, z) # Returns float log(y) base z
math.sin(x) # Returns sin of x in RADIANS
math.cos(x) # Returns cos of x in RADIANS
math.tan(x) # Returns tan of x in RADIANS
math.degrees(x)# Converts angle x from radians to degrees
math.radians(x)# Converts angle x from degrees to radians
math.pi # Returns Pi (3.14159...)
math.e # Returns Euler's number (2.71828...)
More information and help with these functions can be found HERE. Remember that these documents (PyDocs) can be used during your lab!
Putting it Together
Let’s compute the following equations using Python’s math syntax. I strongly recommend you try these problems before looking at the solutions.
The answer you're looking for is included directly below. The solutions are included at the end of the tutorial. Your answer may differ slightly due to rounding if you approach the problem differently. This is okay!
# Answer
0.15195522325791297
# Answer
1.5619923942103266e+183
# Answer
1.5619923942103266e+183
Great! Now, a hint:
The first major lab is usually almost entirely just working with the math
library, as well as the print
statement.
# Answer for Question 1
import math
ans = (1.0 / 2.0) ** math.e
# Answer for Question 2
import math
top = 2 ** (637 - (3213.0/321))
bottom = math.e**12.77777
ans = top/bottom
# OR
import math
ans = (2 ** (637 - (3213.0/321)))/(math.e**12.77777)
# Answer for Question 3
import math
e = math.e
ans = e ** (math.sqrt(e / (e ** e)))
# OR
from math import e
ans = e ** (math.sqrt(e / (e ** e)))