ubuntu2004
Numbers and arithmetic
Simple arithmetic
In Python we can add (2 + 3
), subtract (2 - 3
), multiply (2 * 3
) and divide (2 / 3
) numbers. We can also raise a number to a power, e.g. 3 squared is 3**2
.
Try these out in following code. Remember to press ctrl-enter or ctrl-return to run the code cell.
The symbols +
, -
, *
, /
and **
are called arithmetic operators in Python, that is, they "operate" on the numbers to give a result.
Jupyter Notebook output
If we don't use print()
in code cells, a Jupyter Notebook only outputs the last result when the code cell is run. Which means running
only outputs
If we want to output more than one result we need to use print()
like so:
which outputs
Precedence: The order of calculations matter
Multiplication and division takes precedence over addition and substraction. Which means 2 + 3 * 4 = 14
not 20
. So 3 * 4
is done first (takes precedence) then the result is added to 2.
We can override precedence rules by using parentheses. So (2 + 3) * 4 = 20
. The parentheses force Python to calculate (2 + 3)
first then multiply by 4
.
Power takes precedence over multiplication and addition. So 2 * 3**2 = 18
not 36
. To override precedence so that the multiplication 2 * 3
is done first you would do (2 * 3)**2
.
Try this out in the code cell below to convince yourself that order matters.
Number types: integers and floats
Python has two types of numbers: whole numbers called integers (e.g., 0, 1, 7, 234, -5, -1002) and real numbers, called floats, which have a decimal point in them (e.g., 1.5, 5.678, -0.16).
We can find the type of a number by using the function type()
.
Run type(1)
in the code cell below. The output is int
which tells us that Python considers 1
to be an integer.
Now run type(1.0)
in the code cell.
Unexpected? Because there is a decimal point in 1.0
Python considers it as a float and not an integer.
Here's another operation to be careful of. What type of number do you get if you divide 6 by 2?
6 divided by 2 is 3 which is an integer right? Wrong!
Run the following code to see.
Even though both 6 and 2 are integers (as they don't contain a decimal point) their division, by the division operator /
, results in a float. The division operator /
always results in a float.
So how we get an integer result?
Integer division and remainder (modulus)
Consider the following arithmetic:
is 4 remainder 1
is 3 remainder 0
is 6 remainder 3
How can we do this arithmetic in Python?
To get the quotient (i.e., the 4, 3 and 6 in the above) you can use the integer division operator //
like so: 13 // 3
and 6 // 2
, etc.
To get the remainder (i.e., the 1, 0 and 3 in the above) you can use the remainder operator %
(also known as the modulus operator) like so 13 % 3
and 6 % 2
, etc.
Try this in the following code.
Rounding numbers
If we run 1/7
in the following code cell we'll get a very long number.
All those digits after the decimal point are not really necessary, especially when we are working with data collected from experiments or from the field.
We can round the output of arithmetic to a given number of decimal places (dp for short) by using the round()
function. For example, round(1/7, 3)
will round to 3dp.
Very large and very small numbers: Scientific E-notation
The diameter of a bacterium is about 0.0000001 metres. The number of people on the planet is about 7,792,000,000.
Writing out very small and very large numbers like this is tedious and error prone. They are also difficult to read.
In science we use scientific notation to represent such numbers. So 0.0000001 is and 7,792,000,000 is . That is, we use powers of ten to represent these numbers.
Python uses a similar notation called E-notation: It uses e
or E
instead of . The e
stands for exponent. So is
and is
You can use this notation when doing arithmetic as shown in the following code.