Arithmetic Operators

Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations. You will probably already be familiar with these, since they come from basic mathematics.

+  ==>	Addition : adds two operands
-  ==>	Subtraction : subtracts two operands
*  ==>	Multiplication : multiplies two operands
/  ==>	Division (float) : divides the first operand by the second
// ==>	Division (floor) : divides the first operand by the second
%  ==>	Modulus : returns the remainder when first operand is divided by the second
** ==>  Power : Returns first raised to power second

E.g

a=10
b=2 
print('a+b=', a+b)
print('a-b=', a-b)
print('a*b=', a*b)
print('a/b=', a/b)
print('a//b=', a//b)
print('a%b=', a%b)
print('a**b=', a**b)

Output

a+b = 12
a-b= 8
a*b= 20
a/b= 5.0
a//b= 5
a%b= 0
a**b= 100

E.g

a = 10.5
b = 2
a + b = 12.5
a - b = 8.5
a * b = 21.0
a / b = 5.25
a // b = 5.0
a % b = 0.5
a** b = 110.25

10/2 ==> 5.0 
10//2 ==> 2
10.0/2 ==> 5.0 
10.0//2 ==> 5.0

Note

  • / operator always performs floating point arithmetic. Hence it will always returns float value.
  • But Floor division (//) can perform both floating point and integral arithmetic. If arguments are int type then result is int type. If at least one argument is float type then result is float type.
  • We can use +,* operators for str type also.
  • If we want to use + operator for str type then compulsory both arguments should be str type only otherwise we will get error.
>>> "Waytoeasylearn"+100
TypeError: must be str, not int

>>> "Waytoeasylearn"+"100" 
'Waytoeasylearn100'
  • If we use * operator for str type then compulsory one argument should be int and other argument should be str type.
2*"Waytoeasylearn" 

"Waytoeasylearn"*2

2.5*"Waytoeasylearn" ==> TypeError: can't multiply sequence by non-int of type 'float' 

"Waytoeasylearn"*"Waytoeasylearn" ==> TypeError: can't multiply sequence by non-int of type 'str'
  • For any number x, x/0 and x%0 always raises “ZeroDivisionError”
Arithmetic Operators
Scroll to top