Mathematical calculations in Python

In mathematics, an operation is an action where one or more operands with the help of an operator leads to a solution. The operator can be one of the counters and operands can be represented by, for example, a number. In Python, it is very easy to work with mathematical calculations since many operations are built into Python’s standard library.

How to do mathematical calculations in Python?

When solving technical problems, it is common to have to do several numerical calculations. Initiating a calculation in Python is very easy and Python also provides the standard library called  Math that contains several common mathematical calculations.

 

Initiating a calculation in Java is easy as the most common calculation methods are available by default in Java.

Mathematical operations in Python – Example

It is easy to perform mathematical operations in Python. You simply specify what mathematical operation you want to do between two or more numbers, for example:

 

a = 10
b = 3
c = 2

print(a + b)
print(a * b)
print(a + b * c)

The result then becomes:

 

13
30
16

 

Note that Python follows all mathematical rules, as in this case, where multiplication has higher priority than addition.

NOTATION FOR THE MOST COMMON MATHEMATIC CALCULATIONS

Below is a list of numerical operators on the data type double

Calculation

Addition

Subtraction

Multiplication

Division

Modulus

Less than

Greater than

Less than or equal to

Greater than or equal to

Equal to

Not equal to

Notation

+

*

/

%

.<

>

<=

>=

==

!=

Data type results

double

double

double

double

double.

boolean

boolean

boolean

boolean

boolean

boolean

Example, A = 10 and B = 4 

A + B = 14.0

A – B = 6.0

A * B = 40.0

A / B = 2.5

A % B = 2.0

A < B = false

A > B = true

A <= B = false

A >= B = true

A == B = false

A != B = true

Modulo Operations in Python

The operation that is usually a little tricky to understand at first is Modulo, it returns the remainder  a division. Below are some examples of calculations.

 

x = 7;
y = 2; 

mod = x % y; 
# mod then becomes the rest when we divide 7 by 2 -> mod = 1

x = 8; 
y = 4; 

mod = x % y; 
# Here we get mod = 0, because we get no remainder when we divide 8 by 4

x = 9;
y = 5; 

mod = x % y; 
# The remainder becomes -> mod = 4

This were a few examples and if it still feels a bit unclear, enter the code in the Jupyter Notebook and try it out. First, calculate what you think it will be and then use Jupyter to check if that is correct.

Mathematical Calculations - Modulus in Python