Arithmetic Ā¶
Python can perform arithmetic operations just like a calculator, but with the flexibility to handle much more complex calculations. In this notebook, you'll learn how to use Python to perform basic math operations such as addition, subtraction, multiplication, division, and more.
Operator | Operation | Example | Result |
---|---|---|---|
+
|
Addition |
3 + 4
|
7
|
-
|
Subtraction |
7 - 2
|
5
|
*
|
Multiplication |
5 * 6
|
30
|
/
|
Division |
8 / 2
|
4.0
|
**
|
Exponentiation |
2 ** 3
|
8
|
//
|
Floor Division |
7 // 2
|
3
|
%
|
Modulus (Remainder) |
7 % 2
|
1
|
Addition Ā¶
Python adds numbers with the
+
operator.
For example, we can add two numbers and get the result by printing.
print(3 + 4)
7
There is no reason to only stop at two numbers.
print(3 + 4 + 5)
12
What about decimals?
print(3.14 + 0.4829209)
3.6229209
Surely it cannot add zero.
print(0 + 0)
0
Oh, it can. Neat.
Subtraction Ā¶
We can also do some subtraction.
print(7 - 2)
5
This still works on decimals!
print(73.92 - 1.3892)
72.5308
Multiplication Ā¶
In Python, a single
*
symbol means multiplication.
print(5 * 6)
30
Oh how I wish I had this when learning the multiplication tables. I also don't have to worry about computing how much to tip.
print(0.2 * 32.4)
6.48
Division Ā¶
In Python, the
/
symbol means division.
I wonder how many slices of pizza each person can have if I share with
3
people.
print(8 / 2)
4.0
Well, I guess not. Maybe next time.
Exponents Ā¶
So far, we have had similar symbol operations for math; however, python uses
**
to mean the exponent.
For example, $2^3$ would be computed in Python as.
print(2**3)
8
Now, sometimes you have to deal with pretty large numbers in scientific notation, like $1.3 \times 10^{4}$. We can use our exponents and multiplication.
print(1.3 * 10**4)
13000.0
I do not technically need to put
(7)
, but it does not hurt.
print(1.3 * 10 ** (4))
13000.0
However, if I want to do something like $1.3 \times 10^{7 * 0.5}$, I definitely need the
()
.
print(1.3 * 10**7 * 0.5) # This is wrong!
6500000.0
print(1.3 * 10 ** (7 * 0.5)) # This is right!
4110.960958218893
It can get really tedious to keep typing
1.3 * 10**2
, so Python has a shortcut to just type
1.3e2
.
print(1.3 * 10**2)
130.0
print(1.3e2)
130.0
Math library Ā¶
Python may have a lot of built in calculator stuff, but it does not have everything.
For things like $\cos(\pi)$ or $e^{0.2}$ we need to import the
math
library
.
Do not worry about what a library is yet, we will get there later.
At the moment, you just need to know that you need to tell Python you want to use the
math
library.
We do this by using the
import
statement.
You can get information of what is inside this math library by going to its documentation .
import math
print(math.cos(math.pi))
-1.0
print(math.exp(0.2))
1.2214027581601699
š Exercises Ā¶
Here are some exercises you can do to practice. Open this document in Google Colab and write your Python code in the cells below.
Sales tax Ā¶
If this cute blouse costs $32.45, how much will you pay at the register if sales tax is 6%?
Answer: 34.37
Volume of a cylinder Ā¶
If I have a cylinder with a radius of 0.2 cm and length of 4 cm, what is the volume?
Answer: 0.503 cm 3
Number of atoms Ā¶
Suppose I have 0.3 g of glucose (C 6 H 12 O 6 ) with a molar mass of 180.156 g/mol. How many Carbon atoms are there?
Answer: 6.01 Ć 10 21