Introduction to Python modules XI

Introduction to Python Modules XI

1. What is a Module?

A module is a file that contains Python code—functions, variables, or classes—that you can use in your program.
Modules help in:

  • Reusing code
  • Organizing large programs
  • Reducing complexity

Python has many built-in modules like math, random, statistics, etc.

2. Importing a Module

A. Using import <module>

This statement loads the entire module.

Syntax

import module_name

Usage

To use any function or constant, write:

module_name.function_name()

Example

import math
print(math.pi)
print(math.sqrt(25))

Here we must use the prefix math. before functions/constants.

3. Importing Specific Items Using from Statement

Sometimes we need only specific functions or constants.
For this, we use:

Syntax

from module_name import item1, item2, ...

Example

from math import sqrt, pi
print(pi)
print(sqrt(36))

Now we can directly use sqrt and pi without typing math. each time.

Importing all items

from math import *

Note: Not recommended because it can cause name conflicts.

4. The math Module

The math module contains mathematical constants and functions.

Important constants

ConstantMeaning
math.piValue of π (3.14159…)
math.eEuler’s number (2.71828…)

Important math functions

FunctionDescriptionExample
sqrt(x)Square root of xmath.sqrt(25) → 5
ceil(x)Smallest integer ≥ xmath.ceil(7.2) → 8
floor(x)Largest integer ≤ xmath.floor(7.8) → 7
pow(x, y)x raised to power ymath.pow(2, 3) → 8.0
fabs(x)Absolute value (float)math.fabs(-9) → 9.0
sin(x)Sine of angle x (in radians)math.sin(math.pi/2) → 1
cos(x)Cosine of angle xmath.cos(0) → 1
tan(x)Tangent of angle xmath.tan(math.pi/4) → 1

5. The random Module

The random module is used to generate random numbers.

FunctionDescriptionExample
random()Returns a random float between 0 and 1random.random()
randint(a, b)Returns random integer between a and b (both included)random.randint(1, 10)
randrange(start, stop)Random number from start to stop-1random.randrange(1, 10)
choice(sequence)Random element from a sequencerandom.choice([1, 2, 3])
shuffle(list)Randomly shuffles a listrandom.shuffle(my_list)

Example

import random

print(random.random())
print(random.randint(1, 6))   # dice roll

Summary

  • A module is a Python file containing functions, variables, etc.
  • Use import module to import the whole module.
  • Use from module import item to import specific functions/constants.
  • The math module provides mathematical constants like pi and e and functions like sqrt, ceil, floor, pow, fabs, sin, cos, and tan.
  • The random module helps generate random integers, floats, and selections.

Here are 20 MCQs based on Python Modules for Class 11 Computer Science, with answer key included at the end.


20 MCQs on Python Modules

1. A Python module is:

a) A type of loop
b) A file containing Python code
c) A variable
d) A built-in constant

2. Which statement is used to import a module?

a) include
b) using
c) import
d) module

3. What is the correct way to import the math module?

a) include math
b) import math
c) import.math
d) using math

4. Which statement imports only sqrt from the math module?

a) import sqrt from math
b) from math import sqrt
c) import math.sqrt
d) include math.sqrt

5. Which of the following imports ALL functions of the math module?

a) from math import all
b) import math *
c) import all from math
d) from math import *

6. math.pi represents:

a) 2.718
b) Square root of 2
c) 3.14159…
d) Value of radius

7. Which function gives the square root of a number?

a) math.root()
b) math.sq()
c) math.sqrt()
d) math.power()

8. Output of math.ceil(5.1) is:

a) 5
b) 6
c) 5.1
d) Error

9. Output of math.floor(5.9) is:

a) 5
b) 6
c) 5.9
d) 4

10. Which function gives absolute value as float?

a) abs()
b) math.abs()
c) math.fabs()
d) absolute()

11. Which random function returns a float between 0 and 1?

a) randint()
b) random()
c) randrange()
d) choice()

12. random.randint(1, 10) returns:

a) Any integer from 1 to 10
b) Only 10
c) Only 1
d) Decimal numbers

13. random.choice([10, 20, 30]) returns:

a) 10
b) 20
c) 30
d) Any one of the three values

14. Which function randomly rearranges the elements of a list?

a) random.mix()
b) random.order()
c) random.shuffle()
d) random.change()

15. What is the output of math.pow(2, 3)?

a) 6
b) 8
c) 2^3 (string)
d) 23

16. The math.sin() function takes input in:

a) degrees
b) radians
c) percentage
d) decimal only

17. Which statement is true?

a) from math import pi imports the entire math module
b) import math allows accessing functions using prefix math.
c) from math import * does not import constants
d) import math imports only one function

18. Which is the correct usage after writing import math?

a) sqrt(16)
b) math.sqrt(16)
c) math->sqrt(16)
d) math::sqrt(16)

19. Which random function returns a random integer within a range but excludes the upper limit?

a) randint()
b) random()
c) randrange()
d) choose()

20. Which import method helps avoid writing the module name repeatedly?

a) import module
b) from module import function
c) import module as module
d) using module

Answer Key

1-b
2-c
3-b
4-b
5-d
6-c
7-c
8-b
9-a
10-c
11-b
12-a
13-d
14-c
15-b
16-b
17-b
18-b
19-c
20-b


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top