[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

# Mathematical calculations in Python

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1570684141222{background-color: #f6f6f6 !important;}”]

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.

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]

## How to do mathematical calculations in Python?

[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]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.

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

## 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.[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

## NOTATION FOR THE MOST COMMON MATHEMATIC CALCULATIONS

Below is a list of numerical operators on the data type double[/vc_column_text][vc_empty_space height=”20px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”1/4″][vc_column_text]Calculation

Subtraction

Multiplication

Division

Modulus

Less than

Greater than

Less than or equal to

Greater than or equal to

Equal to

Not equal to[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/4″][vc_column_text]

Notation

+

*

/

%

.<

>

<=

>=

==

!=

[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/4″][vc_column_text]

Data type results

double

double

double

double

double.

boolean

boolean

boolean

boolean

boolean

boolean

[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/4″][vc_column_text]

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

[/vc_column_text][/vc_column_inner][/vc_row_inner][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

## 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.[/vc_column_text][vc_empty_space height=”20px”][vc_single_image image=”19375″ img_size=”full” alignment=”center” onclick=”img_link_large” img_link_target=”_blank” qode_css_animation=””][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”20px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

###### Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]