Chapter 9 Operators in Python Solutions
Question - 21 : - What do you mean by statement in Python?
Answer - 21 : -
A statement is an instruction that the Python interpreter can execute. We have seen two kinds of statements: print and assignment.
When you type a statement on the command line, Python executes it and displays the result, if there is one. The result of a print statement is a value. Assignment statements don’t produce a result.
Question - 22 : - What do you mean by expression in Python ?
Answer - 22 : -
An expression is a combination of values, variables, and operators. If you type an expression on the command line, the interpreter evaluates it and displays the result:
Question - 23 : - Explain Operators and Operands?
Answer - 23 : -
Operators are special symbols that represent computations like addition and multiplication. The values the operator uses are called Operands.
Question - 24 : - Write a example of comparison operators.
Answer - 24 : -
Following example to understand all the comparison operators available in Python programming language:
# !/usr/bin/python
a = 21
b = 10
c = 0
if (a = = b ):
print “Line 1 – a is equal to b”
else:
print “Line 1 – a is not equal to b”
if ( a ! = b ):
print “Line 2 – a is not equal to b”
else:
print “Line 2 – a is equal to b”
if (a <> b):
print “Line 3 – a is not equal to b”
else:
print “Line 3 – a is equal to b”
if ( a < b ):
print “Line 4 – a is less than b”
else:
print “Line 4 – a is not less than b”
if (a > b ):
print “Line 5 – a is greater than b”
else:
print “Line 5 – a is not greater than b”
a = 5;
b = 20;
if (a < = b):
print “Line 6 – a is either less than or equal to b”
else:
print “Line 6 – a is neither less than nor equal to b”
if (b > = a):
print “Line 7 – b is either greater than or equal to b”
else:
print “Line 7 – b is neither greater than nor equal to b”
When you execute the above program it produces following result:
Line 1 – a is not equal to b
Line 2 – a is not equal to b
Line 3 – a is not equal to b
Line 4 – a is not less than b
Line 5 – a is greater than b
Line 6 – a is either less than or equal to b
Line 7 – b is either greater than or equal to b
Question - 25 : - Give an example of arithmetic operators.
Answer - 25 : -
Following example to understand all the arithmetic operators available in Python programming language:
# !/usr/bin/python
a = 21
b = 10
c = 0
c = a + b
print “Line 1 – Value of c is “, c
c = a -b
print “Line 2 – Value of c is “, c
c = a * b
print “Line 3 – Value of c is “, c
c = a/b
print “Line 4 – Value of c is “, c
c = a % b
print “Line 5 – Value of c is “, c
a = 2
b = 3
c = a**b
print “Line 6 – Value of c is “, c
a = 10
b = 5
c = a//b
print “Line 7 – Value of c is “, c
When you execute the above program it produces following result:
Line 1 – Value of c is 31
Line 2 – Value of c is 11
Line 3 – Value of c is 210
Line 4 – Value of c is 2
Line 5 – Value of c is 1
Line 6 – Value of c is 8
Line 7 – Value of c is 2
Question - 26 : - Write a program in Python to explain assignment operators
Answer - 26 : -
Following example to understand all the assignment operators available in Python programming language :
# !/usr/bin/python
a = 21
b = 10
c = 0
c = a + b
print “Line 1 – Value of c is “, c
c += a
print “Line 2 – Value of c is “, c
c *= a
print “Line 3 – Value of c is “, c
c/= a
print “Line 4 – Value of c is “, c
c = 2
c %= a
print “Line 5 – Value of c is “, e
c**= a
print “Line 6 – Value of c is “, c
c//= a
print “Line 7 – Value of c is “, c
When you execute the above program it produces following result:
Line 1 – Value of c is 31
Line 2 – Value of c is 52
Line 3 – Value of c is 1092
Line 4 – Value of c is 52
Line 5 – Value of c is 2
Line 6 – Value of c is 2097152
Line 7 – Value of c is 99864
Question - 27 : - Write source code in Python to explain bitwise operators.
Answer - 27 : -
Following example to understand all the bitwise operators available in Python programming language:
# !/usr/bin/python
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b; # 12 = 0000 1100
print “Line 1 – Value of c is “, c
c = a | b; # 61 = 0011 1101
print “Line 2 – Value of c is “, c
c = a ∧ b; # 49 = 0011 0001
print “Line 3 – Value of c is “, c
c = ~a; # -61 = 1100 0011
print “Line 4 – Value of c is “, c
c = a < < 2; #240 = 1111 0000
print “Line 5 – Value of c is “, c
c = a > > 2; # 15 = 00001111
print “Line 6 – Value of c is “, c
When you execute the above program it produces following result:
Line 1 – Value of c is 12
Line 2 – Value of c is 61
Line 3 – Value of c is 49
Line 4 – Value of c is -61
Line 5 – Value of c is 240
Line 6 – Value of c is 15
Question - 28 : - Write a program in Python to explain logical operators.
Answer - 28 : -
Following example to understand all the logical operators available in Python programming language:
#!/usr/b in/python
a = 10
b = 20
c = 0
if (a and b):
print “Line 1 – a and b are true”
else:
print “Line 1 – Either a is not true or b is not true”
if (a or b):
print “Line 2 – Either a is true or b is true or both are true”
else:
print “Line 2 – Neither a is true nor b is true”
a = 0
if ( a and b ):
print “Line 3 – a and b are true”
else:
print “Line 3 – Either a is not true or b is not true”
if ( a or b ):
print “Line 4 – Either a is true or b
is true or both are true”
else:
print “Line 4 – Neither a is true nor b is true”
if not( a and b) :
print “Line 5 – a and b are true” else:
print “Line 5 – Either a is not true or b is not true”
When you execute the above program it produces following result:
Line 1 – a and b are true
Line 2 – Either a is true or b is true or both are true
Line 3 – Either a is not true or b is not true
Line 4 – Either a is true or b is true or both are true
Line 5 – a and b are true
Question - 29 : - Write source in Python code for membership operators.
Answer - 29 : -
Following example to understand all the membership operators available in Python programming language :
#!/usr/bin/python
a = 10
b = 20
list = [1,2,3,4, 5 ];
if ( a in list):
print “Line 1 – a is available in the given list”
else:
print “Line 1 – a is not available in the given list”
if (b not in list):
print “Line 2 – b is not available in the given list”
else:
print “Line 2 – b is available in the given list”
a = 2
if ( a in list):
print “Line 3 – a is available in the given list”
else:
print “Line 3 – a is not available in the given list”
When you execute the above program it produces following result:
Line 1 – a is not available in the given list
Line 2 – b is not available in the given list
Line 3 – a is available in the given list
Question - 30 : - Write source in Python code for identity operators.
Answer - 30 : -
Following example to understand all the identity operators available in Python programming language :
# !/usr/bin/py thon
a = 20
b = 20
if (a is b):
print “Line 1 – a and b have same
identity”
else:
print “Line 1 – a and b do not have
same identity”
if (id(a) = = id(b)):
print “Line 2 – a and b have
same identity”
else:
print “Line 2 – a and b do not have same identity”
b = 30
if (a is b ):
print “Line 3 – a and b have same
identity”
else:
print “Line 3 – a and b do not have same identity”
if (a is not b ):
print “Line 4 – a and b do not have
same identity”
else:
print “Line 4 – a and b have same identity”
When you execute the above program it produces following result:
Line 1 – a and b have same identity
Line 2 – a and b have same identity
Line 3 – a and b do not have same identity
Line 4 – a and b do not have same identity