Python Tutorial Python Advanced Python References Python Libraries

Python - arithmetic operators example



The example below shows the usage of arithmetic operators - addition(+), subtraction(-), multiply(*), division(/), exponent(**), floor division(//) and modulo(%) operators.

a = 11
b = 2

print("a =",a,", b =",b,"\n");

#Add a and b
result_add = a + b;
print("a + b =", result_add);

#Subtract b from a
result_sub = a - b;
print("a - b =", result_sub);

#Multiply a and b
result_mul = a * b;
print("a * b =", result_mul);

#Divide a by b
result_div = a / b;
print("a / b =", result_div);

#a raised to the power of b
result_pow = a ** b;
print("a ** b =", result_pow);

#returns remainder of division
result_modulo = a % b;
print("a % b =", result_modulo); 

#returns quotient of division
result_fdiv = a // b;
print("a // b =", result_fdiv); 

The output of the above code will be:

a = 11 , b = 2 

a + b = 13
a - b = 9
a * b = 22
a / b = 5.5
a ** b = 121
a % b = 1
a // b = 5

❮ Python - Operators