Breakout: Simple Math in Python

The purpose of this breakout is to help you familiarize yourself with executing Python code in the IPython notebook, and along the way familiarize yourself with some of the mathematical functionality available in Python.

As with many breakouts in this course, you should do the following with a partner. One of you should have your fingers on the keyboard, while the other watches and helps decide what to type.

Some Basic Notebook Navigation

IPython notebooks combine text and executable code: a code cell looks like this, and you can run it by pressing enter/return while holding the shift key, or while holding the Ctrl key. Try it out here, and see what the difference between these is:


In [ ]:
1 + 1

Additionally, you'll sometimes want to insert a new cell to add more code. You can do this by using the [+] button which is second from the left at the top of the notebook page. Before you continue, insert a new cell below this one to make sure you see how it works.

Exploring Arithmetic Operators in Python

Python can be used quite straightforwardly as a calculator. Among other commands, it recognizes +, -, *, /, and () with their typical mathematical meanings. With this in mind, we're going to quickly practice using Python as a calculator.

Use Python to solve the following arithmetic problems:

What is $4235 + 3154$?


In [ ]:

What is $4235 - 3154$?


In [ ]:

What is $4235 \times 3154$?


In [ ]:

What is $\frac{4235}{3154}$?


In [ ]:

What is $\frac{4235 - 3154}{2752 + 5013}$?


In [ ]:

What is $\frac{-(4315 + 378)}{2(65 - 328)}$


In [ ]:

Exploring More Specialized Arithmetic

Some other useful operators are //, %, and **.

What is 4235 // 3154?


In [ ]:

Try the // operator with some other inputs: can you figure out its meaning?


In [ ]:

What is 4235 % 3154?


In [ ]:

Try the % operator with some other inputs: can you figure out its meaning?


In [ ]:


In [ ]:


In [ ]:

What is 4235 ** 3154?


In [ ]:

Try the ** operator with some other inputs: can you figure out its meaning?


In [ ]:


In [ ]:


In [ ]:

Exploring other Operators

There's another class of operators recognized by Python: &, |, ^, and ~.

Play around with these four operators: can you discover their meaning? (Don't worry if you can't: these are quite a bit more obscure than the ones above!).


In [ ]:


In [ ]:


In [ ]:

The math Module

Please switch roles now, so the second person has fingers-on-keyboard

Along with the basic arithmetic operators, Python has some more sophisticated mathematical operations in the math module. A module in Python is a collection of functionality that can be imported and used in your script. Importing functionality from modules is extremely easy in Python compared to some other languages, and it has an incredible array of standard functionality available through these imports.

A popular XKCD comic draws from this idea:

The way you import a module, like in this xkcd comic, is to use the import statement. For example:


In [ ]:
import antigravity

In [ ]:
import math

With this done, we can now access elements of the math module using math.XXX, where we replace XXX with the name of the variable, function, or other object we're interested in.

For example:


In [ ]:
# variable containing the value of π
math.pi

In [ ]:
# function to compute the square root of a number
math.sqrt(2)

Use functions in the math module to compute the following:

What is $\sin(2)$?


In [ ]:

What is $\cos(2)$?


In [ ]:

What is $\exp(2)$ (otherwise known as $e^2$)?


In [ ]:

What is $\log(2)$?


In [ ]:

What is $\sin^2(2) + \cos^2(2)$?


In [ ]:

What is $\sin(2\pi)$? (do you notice anything strange?)


In [ ]:

Exploring Packages with IPython

The IPython notebook includes a very convenient way to explore the contents of a module.

  • If you type math. followed by the tab key, it will give you a list of valid completions, which in this case consists of a list of all functions and variables in the math module.

  • If you type any function followed by a question mark (e.g. math.cos?) then shift-enter, it will give you a documentation string

Use these two convenience features to explore the math module.

Can you find a function that computes $\sqrt{x^2 + y^2}$ efficiently in a single command?


In [ ]:

What is $\sqrt{4235^2 + 3154^2}$?


In [ ]:

What is $e^x - 1$ for $x = 10^{-20}$? (Hint: it's not zero!) Can you find a function in the math module that computes this function accurately?


In [ ]:

Please put a yellow post-it on your laptop, so we know that you've finished the exploration.

While you are waiting for others to finish, continue exploring the math module to see what functionality is available.


In [ ]: