#Example program on All
Arithmetic Operators
a=10 # value 10 is stored in variable a
b=2 # value 2 is stored in variable b
print('a+b=',a+b) #Addition
operator
print('a-b=',a-b)#Subtraction operator
print('a*b=',a*b)#Multiplication
print('a/b=',a/b)#Division operator
print('a//b=',a//b)#Floor Division (integer Division)
print('a%b=',a%b)#Modulus operator Which gives remainder
print('a**b=',a**b)#Exponenet operator
"""output
a+b= 12
a-b= 8
a*b= 20
a/b= 5.0
a//b= 5
a%b= 0
a**b= 100
"""
#Ex2:Arithmetic OPerators
a=10.5
b=2
print(a+b)#12.5
print(a-b)#8.5
print(a*b)#21.0
print(a/b)#5.25
print(a//b)#5.0
print(a%b)#0.5
print(a**b)#110.25
"""output
12.5
8.5
21.0
5.25
5.0
0.5
110.25
"""
Note:
(Devision)/ operator always
performs floating point arithmetic so it will always returns float value. But
floor division or integer division (//) can perform both floating point and
integral arithmetic.
If values are integers then
output is integer. If atleast once value is float then result is float.
If we want to use + operator
for strings, then both parameters should be strings only otherwise we will get
error.
Example:
>>>'rajendra'+15 #Error
>>> 'rajendra'+'15' #ok
'rajendra15'
>>>
Ex2: If we use * operator for
strings then we must use one parameter should be integer and other parameter
should be string type
>>> "rajendra"*10
'rajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendra'
>>>
10*"rajendra"
'rajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendrarajendra'
We can’t multiply sequence by
non-integer type ‘float’
Example:
>>>
2.5*"rajendra" #Error
>>>
Note:
We can not perform zero
division, If we perform zero division it leads to error.
Example:
>>> a=5
>>> a/0 #Error
Division by Zero
Evaluate the
following expression:
d=(x+y)*z**a//b+c
where x=1;y=2;z=3;a=2;b=2;c=3
>>> d=(1+2)*3**2//2+3
d=3*3**2//2+3 #parenthesis evaluated
d=3*9//2+3 #Exponential operator is evaluated
d=27//2+3 #Multiplication, division, and
floor divisions are at equal priority
d=13+3 #addition and subtraction are
afterwards
d=16 #finally assignment is
performed, the value is 16 now stored in variable ‘d’
>>> print(d)
16
>>>
Related Videos:https://youtu.be/6whfA-ARyMQ
Next Topic: Relational Operator