Skip to main content

Calculation

Expressionโ€‹

Like mathematical expression, python expression is the combination of numbers, values, string or any math operations. Expressions are the instructions of variables and operators to obtain a single value. An example of the expression in Python language is

x = 7
y = 4
z = x + y
print(z)

Output:

11

Python can do calculations.

Additionโ€‹

To add two numbers and print the result:

>>> x = 10
>>> y = 4
>>> z = x + y
>>> print(z)
14

Subtractionโ€‹

To subtract two numbers and print the result:

>>> x = 7
>>> y = 5
>>> z = x - y
>>> print(z)
2

Multiplicationโ€‹

To multiply two numbers and print the result:

>>> x = 8
>>> y = 5
>>> z = x * y
>>> print(z)
40

Divisionโ€‹

To divide two numbers and print the result:

>>> x = 12
>>> y = 3
>>> z = x / y
>>> print(z)
4.0

Exponentโ€‹

The ** operator returns, x raised to the power of y. To calculate exponents:

>>> x = 2
>>> y = 3
>>> z = x ** y
>>> print(z)
8