Time for action – using Python as a calculator

We can use Python as a calculator as follows:

  1. In a Python shell, add 2 and 2 as follows:
    >>> 2 + 2
    4
    
  2. Multiply 2 and 2 as follows:
    >>> 2 * 2
    4
    
  3. Divide 2 and 2 as follows:
    >>> 2/2
    1
    
  4. 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.

Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.