Chapter 4: Introduction to Python Modules
Imported Modules
1 import random
2 import math
Question 1: Theory-based Questions
Answer: From short notes (-ve tempo) and check out MCQ questions, solved
problems, and checkpoints.
Question 2: Write Python codes for the following expressions
1 # 1) sqrt(a^2 + b^2 + c^2)
2 print(math.sqrt(a*a + b*b + c*c)) # OR print((a*a + b*b +
c*c)**0.5)
3
4 # 2) 2 - y * e^(2y) + 4y
5 print(2 - y * (math.exp(2*y)) + 4*y)
6
7 # 3) (p + q) / ((r + s)^4)
8 print((p + q) / (math.pow((r + s), 4)))
9
10 # 4) (cos(x) / tan(x)) + x
11 print((math.cos(x) / math.tan(x)) + x)
12
13 # 5) |e^2 - x|
14 print(math.fabs(math.exp(2) - x))
Question 3: Min and max value of the code
1 import random
2 print(random.randint(3, 10) - 3)
Answer: Min: 0, Max: 7
Question 4: WAP to generate a random float between 45 and 95
1 a = random.random() + random.randrange(45, 96)
2 b = math.ceil(a)
3 print(a, b, sep=’ ’)
Example Output:
45.123456789 46
1
Question 5: WAP to generate 2 random numbers between 450
and 950
1 a = random.randrange(450, 951)
2 b = random.randrange(450, 951)
3 avg = (a + b) / 2
4 print(a, b, avg, sep=’\n’)
Example Output:
500
600
550.0
Question 6: WAP to generate 3 random integers with step 13
1 a1 = random.randrange(10, 70, 13)
2 a2 = random.randrange(10, 70, 13)
3 a3 = random.randrange(10, 70, 13)
4 setbe = {a1, a2, a3}
5 print(a1, a2, a3, sep=’ ’)
6 print(setbe)
Example Output:
23 49 10
{10, 23, 49}
Question 7: Statistics module functions
Answer: stat.mode(), stat.median(), stat.mean()
Question 8: Output of the code
1 import random
2 A = [20, 30, 40, 50, 60, 70]
3 L = random.randint(1, 3)
4 U = random.randint(2, 4)
5 for i in range(L, U + 1):
6 print(A[i], end=’#’)
Answer: b) 30#40#50#
Question 9: Output of the code (multiple correct possible)
1 m = [5, 10, 15, 20, 25, 30]
2 for i in range(1, 3):
3 f = random.randint(2, 5) - 1
4 s = random.randint(3, 6) - 2
2
5 t = random.randint(1, 4)
6 print(m[f], m[s], m[t], sep=’#’)
Answer: a) and b)
Question 10: Output of the code
1 import random
2 for i in range(2, 5, 2):
3 print(random.randrange(1, i), end=’#’)
Answer: d) 1#3#
Question 11: What is wrong in the code
1 from math import factorial
2 print(math.factorial(5))
Answer: When importing only the factorial function, you should use print(factorial(5))
directly, not math.factorial(5).
Question 12: WAP to simulate a dice
1 a = ’y’
2 while a == ’y’:
3 b = random.randrange(1, 7)
4 d = print(’dice shows : ’, b)
5 c = input(’do u want to continue (y/n)’)
6 if c == a:
7 continue
8 else:
9 break
Example Output:
dice shows : 4
do u want to continue (y/n)
Question 13: Suggested options and min/max values
1 import random
2 print(int(20 + random.random() * 5), end=’ ’)
3 print(int(20 + random.random() * 5), end=’ ’)
4 print(int(20 + random.random() * 5), end=’ ’)
5 print(int(20 + random.random() * 5))
Answer: Correct options: c) and d), Least value: 20, Max value: 24
3
Question 14: Output of the code
1 import statistics as st
2 v = [7, 8, 8, 11, 7, 7]
3 m1 = st.mean(v)
4 m2 = st.mode(v)
5 m3 = st.median(v)
6 print(m1, m2, m3)
Output:
8 7 7.5
Question 15: WAP to generate 3 random integers divisible by 5
1 for i in range(3):
2 print(int(random.randrange(100, 1000, 5)))
Example Output:
105
305
505