0% found this document useful (0 votes)
17 views4 pages

Python Modules: Random & Math Functions

Uploaded by

Nabeel Khan
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)
17 views4 pages

Python Modules: Random & Math Functions

Uploaded by

Nabeel Khan
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

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([Link](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 * ([Link](2*y)) + 4*y)
6

7 # 3) (p + q) / ((r + s)^4)
8 print((p + q) / ([Link]((r + s), 4)))
9

10 # 4) (cos(x) / tan(x)) + x
11 print(([Link](x) / [Link](x)) + x)
12

13 # 5) |e^2 - x|
14 print([Link]([Link](2) - x))

Question 3: Min and max value of the code


1 import random
2 print([Link](3, 10) - 3)

Answer: Min: 0, Max: 7

Question 4: WAP to generate a random float between 45 and 95


1 a = [Link]() + [Link](45, 96)
2 b = [Link](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 = [Link](450, 951)
2 b = [Link](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 = [Link](10, 70, 13)
2 a2 = [Link](10, 70, 13)
3 a3 = [Link](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: [Link](), [Link](), [Link]()

Question 8: Output of the code


1 import random
2 A = [20, 30, 40, 50, 60, 70]
3 L = [Link](1, 3)
4 U = [Link](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 = [Link](2, 5) - 1
4 s = [Link](3, 6) - 2

2
5 t = [Link](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([Link](1, i), end=’#’)

Answer: d) 1#3#

Question 11: What is wrong in the code


1 from math import factorial
2 print([Link](5))

Answer: When importing only the factorial function, you should use print(factorial(5))
directly, not [Link](5).

Question 12: WAP to simulate a dice


1 a = ’y’
2 while a == ’y’:
3 b = [Link](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 + [Link]() * 5), end=’ ’)
3 print(int(20 + [Link]() * 5), end=’ ’)
4 print(int(20 + [Link]() * 5), end=’ ’)
5 print(int(20 + [Link]() * 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 = [Link](v)
4 m2 = [Link](v)
5 m3 = [Link](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([Link](100, 1000, 5)))

Example Output:
105
305
505

You might also like