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.
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. Try it out here:
In [1]:
1 + 1
Out[1]:
In [2]:
2 + 3
Out[2]:
In [3]:
4 * 5
Out[3]:
In [4]:
5 / 7
Out[4]:
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.
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 [5]:
4235 + 3154
Out[5]:
What is $4235 - 3154$?
In [6]:
4235 - 3154
Out[6]:
What is $4235 \times 3154$?
In [7]:
4235 * 3154
Out[7]:
What is $\frac{4235}{3154}$?
In [8]:
4235 / 3154
Out[8]:
What is $\frac{4235 - 3154}{2752 + 5013}$?
In [9]:
(4235 - 3154) / (2752 + 5013)
Out[9]:
What is $\frac{-(4315 + 378)}{2(65 - 328)}$
In [10]:
- (4315 - 378) / (2 * (65 - 328))
Out[10]:
What is 4235 // 3154
?
In [11]:
4235 // 3154
Out[11]:
Try the //
operator with some other inputs: can you figure out its meaning?
In [12]:
5 // 3
# This operator is "floor division": it's division while dropping the remainder
# 3 goes into 5 _once_ (with a remainder of two)
Out[12]:
What is 4235 % 3154
?
In [13]:
4235 % 3154
Out[13]:
Try the %
operator with some other inputs: can you figure out its meaning?
In [14]:
5 % 3
# This operator is the "modulus": it computes the remainder after division
# Here it returns the remainder of two
Out[14]:
What is 4235 ** 3154
?
In [15]:
4235 ** 3154
Out[15]:
Whoah!!! I should add that this is something Python does easily that other languages choke on: it has variable precision integers that can expand to many, many digits
Try the **
operator with some other inputs: can you figure out its meaning?
In [16]:
2 ** 3
# this is how you express exponentiation in Python
# 2 raised to the power of 3 is 8
Out[16]:
In [17]:
1 & 2
Out[17]:
In [18]:
1 | 2
Out[18]:
These are bitwise operators. To understand what they do, think about the numbers in binary:
$1 = 0 * 2^2 + 0 * 2^1 + 1 * 2^0$ = b001
$2 = 0 * 2^2 + 1 * 2^1 + 0 * 2^0$ = b010
$3 = 0 * 2^2 + 1 * 2^1 + 1 * 2^0$ = b011
The &
operator is a binary AND: it returns all bits for which both numbers have a 1:
In [19]:
1 & 3
Out[19]:
The |
operator is a binary OR: it returns all bits for which either number has a 1:
In [20]:
1 | 3
Out[20]:
The ^
operator is a binary XOR: it returns all bits for which one but not both has a 1:
In [21]:
1 ^ 3
Out[21]:
The ~
operator is a binary NOT: it flips all bits in the number (including the sign!)
In [22]:
~2
Out[22]:
math
ModulePlease 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 [23]:
import antigravity
In [24]:
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 [25]:
# variable containing the value of π
math.pi
Out[25]:
In [26]:
# function to compute the square root of a number
math.sqrt(2)
Out[26]:
Use functions in the math module to compute the following:
What is $\sin(2)$?
In [27]:
math.sin(2)
Out[27]:
What is $\cos(2)$?
In [28]:
math.cos(2)
Out[28]:
What is $\exp(2)$ (otherwise known as $e^2$)?
In [29]:
math.exp(2)
Out[29]:
What is $\log(2)$?
In [30]:
math.log(2) # log base e
Out[30]:
In [31]:
math.log10(2) # log base 10
Out[31]:
In [32]:
math.log2(2) # log base 2
Out[32]:
What is $\sin^2(2) + \cos^2(2)$?
In [33]:
math.sin(2) ** 2 + math.cos(2) ** 2
Out[33]:
What is $\sin(2\pi)$? (do you notice anything strange?)
In [34]:
math.sin(2 * math.pi)
# It's close to zero, but not exact as we might expect!
# This is an issue of floating point roundoff error.
Out[34]:
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 [35]:
help(math.hypot)
What is $\sqrt{4235^2 + 3154^2}$?
In [36]:
math.hypot(4235, 3154)
Out[36]:
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 [37]:
math.expm1(10 ** -20)
Out[37]:
While you are waiting for others to finish, continue exploring the math
module to see what functionality is available.
(Recall: you can type math.
followed by the TAB key to see what's available, and you can put a question mark after any Python object to get its documentation string)
In [ ]: