Ch-1,2,3 Back Exercises
Ch-1,2,3 Back Exercises
4. Evaluate the following expressions involving arithmetic, relational, and logical operators:
(a) 5 % 10 + 10 < 50 and 29 >= 29
(b) 7 ** 2 <= 5 // 9 % 3 or 'bye' < 'Bye'
(c) 5 % 10 < 10 and - 25 > 1 * 8 / | 5
(d) 7 ** 2 // 4 +5 > 8 or 5 != 6
(e) 7 / 4 <6 and 'I am fine' > 'I am not fine'
(f) 10 + 6 * 2 2 != 9 // 4 - 3 and 29 >= 29 / 9
(g) 'hello' * 5 > "hello' or 'bye' < 'Bye'
5. Evaluate the following expressions involving bitwise operators:
(a) 15 & 22
(b) 1 5 |22
(c) -15 & 22
(d) -15 | 22
(e) ~15
() ~22
(g) ~-20
(h) 15 ^ 22
(i) 8 << 3
rammung
) 40 >> 3
7. What output will be displayed when the following commands are executed in Pytho
shell in sequence:
(a) >>> a= 6
>>> a == 6
>>> a < 5.9
>>> a > 5.9
(b) >>> b= 7
>>> b /6
>>> b //6
>>> b/ 4
>>> b 4
>>> b %7
>>> b * 2
>>> b ** 2
2xy - 9y 4yx?
(b) result =
2xy 2y
1
(x - v) + e* - 1 - + tan x
(c) result = 2 cos (x + y) cos 2
4
2
log (v)
twostatements differ?
|. Howdoes the effect of the following
(a) x t= x + 10
(b) x = X t 10
44
Python Programming
import random
X =
random.random () + 5
3. Evaluate the following expressions using Python shell. Assume that ASCII coding
scheme is used for character data.
abs (-5.4) abs (15) chr (72)
round (-24.9) float (57) complex ('1+2j')
divmod (5,2) float (57) pow (9, 2)
max (97, 88, 60) min (55, 29, 99)
max ('a', 'b', 'AB')
4. Develop Python functions to produce the following outputs:
(a)
(b)
$ $ $ $$
5. Consider the following function:
def nMultiple (a = 0, num = 1):
return a num
What will be the output produced when the following calls are made:
(a) nMultiple (5)
(b) nMultiple (5, 6)
(c) nMultiple (num = 7)
(d) nMultiple (num = 6, a = 5)
(e) nMultiple (5, num = 6)
6. Study the program segments given below. Give the output produced, if any.
(a) def test (a, b):
a = a+b
b = a-b
a = a-b
print('a = a)
print ('b = ', b)
test (5, 8)
(b) def func ():
pass
a = func ()
print (a)
7. Write a function areaTriangle that takes the lengths of three sides: sidel, side2
and side3 of the triangle as the input parameters and returns the area of the triangle a
Functions 45
the output. Also, assert that sum of the length of any two sides is greater than the third
side. Write a function main that accepts inputs fromthe user interactively and computes
the area of the triangle using the function areaTriangle.
8. Create the following scripts importedModule (Fig. 2. 17) and mainModule
(Fig. 2.18) in the working directory, execute the script mainModule and justify the
output.
01 def testl () :
02 print ('testl in impor ted module')
03
04 def test2 () :
05 print (' test2 in imported module')
06
07 testl )
08 test2 ()
01 import importedModule
02 print ('hello')
Fig. 2.18 (mainModule.py)
9 Rewrite the code in question 7 so that it takes inputs as command line arguments.
80 Python Programminy
while <test-condition>:
<Sequence S of statements>
Here, test-condition is aBoolean expression which isevaluated at the beginnino
of the loop. If the test -condition cvaluates to True, the control flows through
the Sequence S of statements (i.e., the body of the loop). otherwise the
control moves to the statement immediately following the while loop. On execution
of the body of the loop, the test-condition is evaluated again, and the
process of
evaluating the test -condi tion and executing the body of the loop is continued until
the test-condition evaluates to False.
8. The break statement is used for exiting from the
loop to the statement following the
body of the loop.
9. The continue statement is used to transfer the control to next iteration of the loon
without executing rest of the body of loop.
10. The pass statement lets the program go through this
any action.
piece of code without performing
11. The else clause can be used with
for and while loop. Statements specified in
else clause willbe executed on smooth
termination of the
terminated using break statement, then the else clause isloop. However, if the loop is
skipped.
EXERCISES
1. Write an assignment
if-else code:
statement using a single conditional expression for the followin
if marks >=70:
remarks = 'good
else:
remarks = 'Average!
2. Study the program segments given
below. In each case, give the output
(a) total = 0 produced, if any
Count = 20
while count > 5:
total += count
Count -= 1
print (total)
(b) total = 0
N = 5
for i in range (1 N+1):
for j in range (1, i+l):
total += 1
print (total)
Control Structures 81
(c) total = 0
N = 10
for i in range (1, N+1) :
for j in range (1, N+1) :
total += 1
print (total)
(d) total = 0
N= 5
for i in range (1, N+1) :
for j in range (1, i+l) :
total += 1
total -=1
print (total)
(e) total = 0
N = 5
for i in range (1, N+1) :
for j in range (1, N+l) :
total += i
print (total)
(f) total =0
N=5
for i in range (1, N+1) :
for j in range (1, i+1) :
total t= j
print (total)
(g) total =0
N= 5
for i in range (1, N+1):
for j in range (1, N+1):
total t= it j
print (total )
(h) total = 0
N=5
for i in range (1, Nt1) :
for j in range (1, i+1) :
for k in range (1, j+l):
total t= 1
print (total)
(a) (b)
1
2
2 1 2
2 3
3 2 1 2 3
2 3 4 4 3 1
2 2 3 4
3 4 5
(c)
5 4 3 2 1 1
4 3 2 1 2
3 2 2
1
2 3 3 3
1
4 4 4 4
5 5 5 5 5
(e)
1 2
(f
3 4 5
2 3 4 5
3 4 5
4 5
5
(Cont'd)
Table (Continued)
(i)
(k) ()
m)