0% found this document useful (0 votes)
42 views8 pages

Ch-1,2,3 Back Exercises

This document provides examples of Python programming concepts including expressions, operators, functions, control structures like loops and conditionals. It contains exercises to evaluate expressions, write functions, and use control structures like while and for loops. The key topics covered are arithmetic, relational, logical and bitwise operators, functions, loops, and conditional statements.

Uploaded by

Nìkèt Sαínì
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views8 pages

Ch-1,2,3 Back Exercises

This document provides examples of Python programming concepts including expressions, operators, functions, control structures like loops and conditionals. It contains exercises to evaluate expressions, write functions, and use control structures like while and for loops. The key topics covered are arithmetic, relational, logical and bitwise operators, functions, loops, and conditional statements.

Uploaded by

Nìkèt Sαínì
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Python Programming: An Introduction 17

from nonlocal while and del global


not With if Or yield
assert else import pass
breakezcept
in raise elif

18. Python programming can be done in aninteractive and script mode.


EXERCISES
1. Evaluate the following expressions:
(x < y) or (not(z == y) and (z < x))
(a) x = 0, y= 6, z = 10
(b) x = 1, y=1, z=1
2. Evaluate the following expressions involving arithmetic operators:
(a) -7 * 20 + 8 / 16 * 2 + 54
(b) 7 ** 2 // 9 % 3
(c) (7 - 4 * 2 ) * 10 /5 ** 2 + 15
(d) 5 % 10 + 10 - 25 * 8 // 5
(e) hello! * 2 - 5
3. Evaluate the following expressions involving relational and logical operators:
(a) 'hi' > hello ! and 'bye' < 'Bye'
(b) 'hi' > 'hello' or 'bye' < 'Bye
(c) 7 > 8 or 5 < 6 and 'I am fine' > 'I am not fine'
(d) 10 != 9 and 29 >= 29
(e) 10 != 9and 29 >= 29 and 'hi' > 'hello' or 'bye' < 'Bye' and
7 <= 2.5

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

6. Differentiate between the following operatorswith the help of examples:


(a) = and ==
(b) / and %
(c) / and //
(d) and * *

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

8. Construct logical expressions for representing the following conditions:


(a) marks scored should be greater than 300 and less than 400.
(b) Whether the value of grade is an uppercase letter.
(c) The post is engineer andexperience is more than four years.
9. Identify Python keywords from the following list of words:
And class PASS if exec PRINT Not

10. Write Python statements for the following equations:


-b+b - 4ac
(a) rootl =
2a

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 ()

Fig. 2.17 (importedModule.py)

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)

(i) number = 72958476


a, b = 0, 0
while (number > 0):
82
Python Programming
digit number % l0
if (digit % 2!= 0):
a t= digit
else:
b += digit
number /= 10
print (a, b)
3. Write a function to determine whether a given natural number is a perfect number. A
natural number is said to be a perfect number if it is the sum of its divisors. For example.
6is aperfect number because 6=1+2+3, but 15 is not a perfect number
because
1541+3+5.
4. Write a function that takes two numbers as input
parameters and returns their least
common multiple.
5. Write a function that takes two numbers as
input parameters and returns their greatest
common divisor.
6. Write a function that accepts as an input
prints a figure like:
parameter the number of rows to be printed and

(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)

the following series:


7. Write a function that finds the sum of the n terms of xn /n!
+..
(a) 1 - x² /2! + x' /4! - x /6!
X
(b) e* =1+ 1! 2! 3!
on whether the given number is
8. Write a function that returns True or False depending
a palindrome.
it as an argument.
9. Write a function that returns the sum of digits of a number, passed to
range I to 1000. An Armstrong
10. Write a program that prints Armstrong numbers in the the number itself.
number is anumber whose sum of the cubes of the digits is equal to
For example, 370 =3+7+ 0'.
returns True or False
11. Write a function that takes two numbers as input parameters and
depending on whether they are co-primes. Two numbers are said to be co-prime if they
do not have any commondivisor other than one.
12. Write a function sqrt that takes a non-negative number as an input and conputes its square
root. We can solve this problem iteratively. You will recall from high school mathematics
that to find the square root of a number, say 2, we need to solve the equation f (x) =
x'- 2= 0. To begin with, choose two numbers a and b so that f(a) <0 and f (b) >0.
Now, for the equation f(x) =x - 2 = 0. f (1)<0 and f (2) >0. So, the root of the
equation must lie in the interval (a,b] (i.e. [1,2]). We find the midpoint, say, midof
the interval a,b). If f (a)<0 and f (mid) >0, we knowthat the root of the equation
f(x) =0 lies in the interval [a, mid]. However, in the other case, (f (mid) <0 and

You might also like