Time for action – using Python as a calculator
We can use Python as a calculator as follows:
- In a Python shell, add 2 and 2 as follows:
>>> 2 + 2 4
- Multiply 2 and 2 as follows:
>>> 2 * 2 4
- Divide 2 and 2 as follows:
>>> 2/2 1
- If you have programmed before, you probably know that dividing is a bit tricky since there are different types of dividing. For a calculator, the result is usually adequate, but the following division may not be what you were expecting:
>>> 3/2 1
We will discuss what this result is about in several later chapters of this book. Take the cube of 2 as follows:
>>> 2 ** 3 8
What just happened?
We used the Python shell as a calculator and performed addition, multiplication, division, and exponentiation.