MENU
Question -

Give example to understand operator precedence available in Python programming language.



Answer -

Following example to understand operator precedence available in Python programming language :

#!/usr/bin/python

a = 20
b = 10
c = 15
d = 5
e = 0
e = (a + b)*c/d #(30*15)/5
print “Value of (a + b) * c / d is “, e

e = ((a + b) * c)/d #(30*15)/5
print “Value of ((a + b) * c) / d is “, e

e = (a + b) * (c / d); # (30) * (15/5)
print “Value of (a + b) * (c / d) is “, e

e = a + (b*c)/d; # 20 + (150/5)
print “Value of a + (b * c) / d is “, e

When you execute the above program it produces following result:

Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
Value of (a + b) * (c / d) is 90
Value of a + (b * c) / d is 50

Comment(S)

Show all Coment

Leave a Comment

Free - Previous Years Question Papers
Any questions? Ask us!
×