Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

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.

# simple arithmetic 2+3*200029**9*6784745**266*0
2

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

2+3 2*3

only outputs

6

If we want to output more than one result we need to use print() like so:

print( 2+3 ) print( 2*3 )

which outputs

5 6

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.

# precedence

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.

# types of numbers type(1.8)
float

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.

type( 6 / 2 )
float

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:

  • 133\frac{13}{3} is 4 remainder 1

  • 62\frac{6}{2} is 3 remainder 0

  • 335\frac{33}{5} 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.

# integer division and remainder print( 13 // 3 ) print( 13 % 3 ) print( type( 6 // 2 ) )
4 1 <class 'int'>

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.

Try this in the following code.
# rounding numbers print(round(1/7, 1)) print(1/7)
0.1 0.14285714285714285

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 1×1061\times 10^{-6} and 7,792,000,000 is 7.792×1097.792\times 10^{9}. 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 ×10\times10. The e stands for exponent. So 1×1061\times 10^{-6} is

1e-6 or 1E-6

and 7.792×1097.792\times 10^{9} is

7.792e9 or 7.792E9

You can use this notation when doing arithmetic as shown in the following code.

# e-notation print( 3.4e7 / 2.3E-9 )
1.4782608695652174e+16

Exercise Notebook

Next Notebook