Arithmetic Operators - Interactive exercises - SOLVED#

Make sure you are running this in Colab!

Everything in a notebook is in cells. Text like what you’re reading right now goes in a ‘Markdown’ cell. Code goes in a code cell, like the one below. Try doing a simple math calculation like \(2+2\), and push Shift+Enter or click ‘Run’ in the top bar to evaluate it.

2+2
4

Now try multiplying, dividing, subtraction. When you evaluate a Python command in a code cell, the result is printed below the cell. You can put multiple lines in a cell by using Enter between them. When you evaluate the cell, the output is only the last value calculated. Try it.

2-2
2*2
4

Most math expressions are straightforward. One slightly unusual thing is the power operator. We code \(x^y\) as x**y. Try using this to:

  1. Find \(2^{10}\).

  2. Find the square root of a million.

  3. Find the cube root of \(4913\).

2**10
1024
1000000**0.5
1000.0
4913**(1./3.)
16.999999999999996

The standard division operator is /, for instance \(1/4 = 0.25\). There is also the integer division operator // and the remainder operator % which are described above.

Exercise Try a few things like 7//3 and 7%3 to see how they work.

30//7, 30%7, 30/7.
(4, 2, 4.285714285714286)

Exercise Demonstrate how these operators (or just one of them!) can be used to quickly evaluate if a number is even or odd

12391872391874 % 2
0

WARNING we will be using // and % for problems in the coming weeks, and will require you to identify when to use them. Still confused about what these do? Ask one of us!