Math Operators in Python

We can perform math operations using Python operators (+, -, *, /, **). The examples below cover all basic math operators available in Python:

Addition

>>> 1 + 3.5 

4.5 

Subtraction

>>> 1 - 3.5 

-2.5 

Multiplication

>>> 1 * 3.5 

3.5 

Division

>>> 1 / 3.5 

0.2857142857142857 

Exponentiation (3 * 3 * 3 * 3)

>>> 3 ** 4 

81 

The order of operations is exponentiation, multiplication, division, addition,subtraction. For example, in the following expression we have exponentiation, multiplication and addition:

>>> 1 + 2 * 10**2 

201 

You get 201 because first we get 100 from 10**2 (exponentiation), and then we get 200 from 2 * 100 (multiplication), and finally 201 from 1 + 200 (addition).

However you can control what to execute first by using parenthesis:

>>> (1 + 2) * 10**2 

300 

You get 300 because first we get 3 from 1 + 2, and then we get 100 from 10**2 and then 300 from 3 * 100.

Math Operator Practice

Please calculate the product (multiplication) of x  and y , raise the product to the power of z  and divide everything by 8. You should print out the output using the print  function. Please add your code below the existing lines.

Tip: You can either create a variable first, store the math expression in the variable, and then print out the variable, or you can simply put the expression inside the print() function directly. Either way is fine.

x = 1
y = 2
z = 3
a = (x * y) ** z / 8
print(a)

 

Python 12

FOLLOW US ON LinkedIn



Explore Tutu'rself