Using Python as a calculator#

Calculating 2+2#

One of the simplest uses of Python is as a calculator.

Open a new Notebook and enter into a code cell

2+2

If you run this cell you should get something that looks like the screenshot below.

The cell has run (indicated by the number 1 to the left of the cell), and the “code” has produced a result, which is printed below the cell. Beneath that you get a new, empty cell for your next piece of code.

Within this course book, cells that have been run are rendered as follows:

2+2
4

You can add space between the numbers to make things easier to read:

2 + 2
4

Or wrap the expression in brackets:

(2 + 2)
4

And still get the same result.

There are three core types of number in Python: Integers (whole numbers), real numbers, and complex numbers. The different types of number correspond to the different ways that Python can represent numeric information.

  • Integers: A number without a decimal point: e.g., 1, 2, 100, 2100.

  • Floats: A number with a decimal point. e.g., 3.14159, 12.0107, 8.3144.

  • Complex: A number with a real and imaginary component. Python uses the symbol j to denote the imaginary component of a complex number. e.g., (1 + 3j), (-1 + 0j).

type(1) # The value "1" represented as an integer.
int
type(1.0) # The value "1" represented as a float.
float
type(1.0+0.0j) # The value "1" represented as a complex number.
complex

Simple arithmetic#

You are not limited to adding numbers. Try entering the following expressions into code cells and executing them (running the cells.):

  • subtraction:

3 - 1

  • multiplication:

4 * 2

  • powers (e.g. 42)

4 ** 2

  • division

6 / 2

In each of these examples you have combined two numbers using an operator (-, *, **, /) to give a single expression. When you run the cell containing this expression, the expression is evaluated and the result is returned as your output.

Notice that the last example gives 3.0 and not 3 as the result, even though 6 and 2 are both integers. The division operator (/) always returns a float, even when one of both of the numbers being operated on are integers.

Arithmetic also works with floats:

2.0 + 2.0

4.0 * 2.0

4.0 ** 2.0

6.0 / 2.0

And with mixtures of integers and floats

2 + 2.0

4 * 2.0

4.0 ** 2

6 / 2.0

floats can also be written without any numbers after the decimal point, e.g.

42.

is the same as 42.0.

You can do integer division (also called floor division ) using the operator //:

6 // 2

Integer division rounds down:

7 // 2

You can calculate the remainder of integer division using the % symbol (called the modulo operator).

7 % 2

8 % 2

Brackets can be used to specify the order of operations in more complicated expressions, according to the usual mathematical rules:

(2 + 3) * 4

2 + (3 * 4)

Mathematical functions#

In addition to the basic algebraic operators above, Python provides many common mathematical functions and the constants \(\pi\) and \(\mathrm{e}\) in the math library. This is a built-in module that is part of the standard Python installation, but it must be imported before use:

import math

To use functions or constants from the math module, we prefix their names with math.

math.pi
3.141592653589793
math.sin(math.pi/4)
0.7071067811865475
math.sqrt(2)/2
0.7071067811865476

For the last two examples, we have used a function call, which looks like the name of the function followed by a pair of parenthesis, which usually (but not always) then contain one or more arguments that the function operates on.

generic_function(argument1, argument2)

Note that the trigonometic functions in math use radians rather than degrees.

The math module can also be used to calculate exponentials and logarithms.

math.exp(2) # e^2
7.38905609893065
math.log(7.38905609893065) # natural log of e^2
2.0

Note that math.log computes the natural logarithm (base \(\mathrm{e}\)) of the argument. If you want to calculate a base 10 logarithm you can use math.log10:

math.log10(100)
2.0

Python also provides a couple of useful built-in functions: abs and round.

abs(-4) # returns the absolute value of the argument.
4
round(math.pi) # rounds down to the nearest integer.
3