In this page we’ll look at some objects in Python which are used to represent numbers.

Integers and decimal numbers

The two main ones are: int (integers) and float (floating point or decimal numbers):

>>> A = 12 # this is an integer
>>> type(A)
<type 'int'>
>>> B = 12.5 # this is a float
>>> print(type(B))
<type 'float'>

The only difference in terms of notation if the use of a period for decimal numbers. A number is a decimal number even if there is no digit after the period:

>>> C = 12. # this is still a float
>>> type(C)
<type 'float'>

Arithmetic Operations

Addition

Adding numbers is easy and intuitive: simply use the + (plus) operator :

>>> 1 + 1
2
>>> 1 + 1 + 10
12

Integers can be added to integers, floats can be added to floats, and integers can be added to floats (and vice-versa):

>>> 12 + 13 # result is an integer
25
>>> 12 + 0.5 # result is a float
12.5
>>> 0.5 + 12.5 # result is a float
13.0
  • If both numbers are integers, the result is also an integer.
  • If at least one of the numbers is a float, the result is a float.

Subtraction

Subtracting numbers follows the same logic, just use the - (minus) operator:

>>> 12 - 8
4

The minus sign is also used to indicate negative numbers:

>>> a = -100
>>> a + 25
-75

Multiplication

In Python, the multiplication operator is the * character, rather than × (multiplication sign) as usual in maths. But it works exactly the same way:

>>> 12 * 8
96
>>> 12 * -25
-300

Division

Division in Python uses the / (forward slash) as its operator:

>> 11 / 2
5.5

Division by zero is mathematically not possible and will always raise an error:

>> 1 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

Integer division can be performed with the special operator //:

>>> 11 // 2
5

Finally, use the % operator to get the rest of a division (modulo):

>>> 11 % 2
1

Exponentiation

To elevate a number to the power of another number, the operator ** is used:

2 ** 8
10 ** 2
2 ** 0.5

Combining operations

Arithmetic operations can be combined into larger statements and calculations:

7 + 10 - 100 * 3 / 200 ** 4

Python executes division and multiplication first, and addition and subtraction afterwards. If you want to add or subtract first, you must put these operations between parentheses – Python will execute operations between parentheses first:

>>> 9 * 9 + 2
83
>>> 9 * (9 + 2)
99

Increment/decrement operators

Sometimes when writing code it is necessary to increment or decrement a value (to add or subtract a value from an integer):

a = 10
# ... something happens here...
a = a + 2

In cases like this, it is possible to write the same line using += to increase the value:

a += 2 # same as: a = a + 2

…and -= to decrease it:

b -= 1 # same as: b = b - 1

There is also a matching *= operator to multiply a value by itself:

c *= 10 # same as: c = c * 10

More math

Python can do many other kinds of mathematical calculations, of course. Many of these mathematical functions live in the math module. For example, trigonometric functions such as sine, cosine, tangent; constants such as pi, etc.

>>> import math
>>> math.pi
3.14159265359
Last edited on 06/12/2018