MENU

Chapter 9 Operators in Python Solutions

Question - 31 : -
Give example to understand operator precedence available in Python programming language.

Answer - 31 : -

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

Question - 32 : -
How are comments written in a program ?

Answer - 32 : -

As the program gets bigger, it becomes difficult to read it, and to make out what it is doing by just looking at it. So it is good to add notes to the code, while writing it. These notes are known as comments. In Python, comment start with ‘#’ symbol. Anything written after # in a line is ignored by interpreter, i.e. it will not have any effect on the program.
For example,
# calculating area of a square > > > area = side * * 2
For adding multi-line comment in a program :
(i) Place ‘#’ in front of each line, or,
(ii) Use triple quoted string. They will only work as comment, when they are not being used as docstring.

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