Simple Mathematical Operators

Introduction

Python does common mathematical operators on its own, including integer and float division, multiplication, exponentiation, addition, and subtraction. The math module (included in all standard Python versions) offers expanded functionality like trigonometric functions, root operations, logarithms, and many more.

Numerical types and their metaclasses

The `numbers` module contains the abstract metaclasses for the numerical types:

subclassesnumbers.Numbernumbers.Integralnumbers.Rationalnumbers.Realnumbers.Complex
bool
int
fractions.Fraction
float
complex
decimal.Decimal

Possible combinations (builtin types):

• `int` and `int` (gives an `int`)
• `int` and `float` (gives a `float`)
• `int` and `complex` (gives a `complex`)
• `float` and `float` (gives a `float`)
• `float` and `complex` (gives a `complex`)
• `complex` and `complex` (gives a `complex`)

Note: the `+` operator is also used for concatenating strings, lists and tuples:

Division

Python does integer division when both operands are integers. The behavior of Python's division operators have changed from Python 2.x and 3.x (see also Integer Division ).

Python 2.x2.7

In Python 2 the result of the ' / ' operator depends on the type of the numerator and denominator.

Note that because both `a` and `b` are `int`s, the result is an `int`.

The result is always rounded down (floored).

Because `c` is a float, the result of `a / c` is a `float`.

You can also use the operator module:

Python 2.x2.2

What if you want float division:

Recommended:

Okay (if you don't want to apply to the whole module):

Not recommended (may raise TypeError, eg if argument is complex):

Python 2.x2.2

The ' // ' operator in Python 2 forces floored division regardless of type.

Python 3.x3.0

In Python 3 the `/` operator performs 'true' division regardless of types. The `//` operator performs floor division and maintains type.

Possible combinations (builtin types):

• `int` and `int` (gives an `int` in Python 2 and a `float` in Python 3)
• `int` and `float` (gives a `float`)
• `int` and `complex` (gives a `complex`)
• `float` and `float` (gives a `float`)
• `float` and `complex` (gives a `complex`)
• `complex` and `complex` (gives a `complex`)

Exponentation

Another difference between the built-in `pow` and `math.pow` is that the built-in `pow` can accept three arguments:

Special functions

The function `math.sqrt(x)` calculates the square root of `x`.

To compute other roots, such as a cube root, raise the number to the reciprocal of the degree of the root. This could be done with any of the exponential functions or operator.

The function `math.exp(x)` computes `e ** x`.

The function `math.expm1(x)` computes `e ** x - 1`. When `x` is small, this gives significantly better precision than `math.exp(x) - 1`.

Inplace Operations

It is common within applications to need to have code like this :

or

There is an effective shortcut for these in place operations :

Any mathematic operator can be used before the '=' character to make an inplace operation :

• `-=` decrement the variable in place
• `+=` increment the variable in place
• `*=` multiply the variable in place
• `/=` divide the variable in place
• `//=` floor divide the variable in place # Python 3
• `%=` return the modulus of the variable in place
• `**=` raise to a power in place

Other in place operators exist for the bitwise operators (`^`, `|` etc)

Logarithms

By default, the `math.log` function calculates the logarithm of a number, base e. You can optionally specify a base as the second argument.

Special variations of the `math.log` function exist for different bases.

Modulus

Like in many other languages, Python uses the `%` operator for calculating modulus.

Or by using the `operator` module:

You can also use negative numbers.

If you need to find the result of integer division and modulus, you can use the `divmod` function as a shortcut:

Multiplication

Possible combinations (builtin types):

• `int` and `int` (gives an `int`)
• `int` and `float` (gives a `float`)
• `int` and `complex` (gives a `complex`)
• `float` and `float` (gives a `float`)
• `float` and `complex` (gives a `complex`)
• `complex` and `complex` (gives a `complex`)

Note: The `*` operator is also used for repeated concatenation of strings, lists, and tuples:

Subtraction

Possible combinations (builtin types):

• `int` and `int` (gives an `int`)
• `int` and `float` (gives a `float`)
• `int` and `complex` (gives a `complex`)
• `float` and `float` (gives a `float`)
• `float` and `complex` (gives a `complex`)
• `complex` and `complex` (gives a `complex`)

Trigonometric Functions

Note that `math.hypot(x, y)` is also the length of the vector (or Euclidean distance) from the origin `(0, 0)` to the point `(x, y)`.

To compute the Euclidean distance between two points `(x1, y1)` & `(x2, y2)` you can use `math.hypot` as follows

To convert from radians -> degrees and degrees -> radians respectively use `math.degrees` and `math.radians`