Introduction to Python Modules
Module : A module is a Python file where functions, constants and variables are defined. Extension of Python module is .py
Advantages of Modules in Python
- It makes the code reusable, means we can use the same code in different programs.
- It makes our code easier to understand.
- It help us to organize or manage our code in Python.
Introduction to Python Modules
Importing Python modules
There are three ways to import a module in Python.
1. Using import statement : This is one of the most common way to use module in Python. This method allow us to access all the functions/objects defined in the module. for example : to use math module, we can write —
import math
To use more than one module like math and random in our code, we can write :
import math, random
After importing module in our code, we can access a specific function by writing:
<module name>.<function name> like math.sqrt
2. Using ‘from’ statement : This method allow us to import specific function of a module. for example
from math import ceil
To import more than one function of a module, we can write
from math import sqrt, fabs
3. Using import* statement : This statement can be used to import all functions of a specific module like
from math import*
Types of Python Modules
There are two types of Python modules:
1. Built-in Modules
2. User defined Modules
1. Built-in Modules : There are many built in modules in Python Library. Some of the common built-in modules are:
- Math module
- Statistics module
- Random module
1. Math module : This module has many mathematical functions like ceil, floor, sqrt etc. Let we discuss few functions of math module.
a. floor(n) : This function returns the largest integer which is less than or equal to n. for example
import math print(math.floor(23.45) Output : 23 import math print(math.floor(-40.36) Output : -41 import math print(math.floor(23.96) Output : 23
Introduction to Python Modules
b. ceil(n) : This function returns the smallest integer which is more than or equal to n. for example
import math print(math.ceil(23.45) Output : 24 import math print(math.ceil(-40.36) Output : -40 import math print(math.ceil(23.96) Output : 24
c. pow(m, n) : This function returns m raise to the power n. for example
import math print(math.pow(2,3)) Output : 8 import math print(math.pow(4,2)) Output : 16
d. sqrt( ) : This function return the square root of a number passed as argument. for example
import math print(math.sqrt(16)) Output : 4.0 import math print(math.sqrt(121)) Output : 11.0 import math print(math.sqrt(-121)) Output : Error
e. fabs( ) : This method return the absolute value(positive value) of a number passed as an argument. for example
import math print(math.fabs(-9)) Output : 9.0 import math print(math.fabs(78)) Output : 78 import math print(math.fabs(-65.78)) Output : 65.78
f. sin( ) : This function returns the sine of value passed as argument. for example
import math print(math.sin(90)) Output : 0.8939966636005579 import math print(math.sin(0)) Output : 0.0 import math print(math.sin(45)) Output : 0.8509035245341184 NOTE : Value passed in this function should be in radian
Introduction to Python Modules
g. cos( ) : This function returns the cosine of value passed as argument. for example
import math print(math.cos(90)) Output : -0.4480736161291701 import math print(math.cos(0)) Output : 1.0 import math print(math.cos(45)) Output : 0.5253219888177297 NOTE : Value passed in this function should be in radian.
h. tan( ) : This function returns the tangent of value passed as argument. for example
import math print(math.tan(90)) Output : -1.995200412208242 import math print(math.tan(0)) Output : 0.0 import math print(math.tan(45)) Output : 1.6197751905438615 NOTE : Value passed in this function should be in radian.
i. pi : It returns mathematical constant. for example
import math print(math.pi) Output : 3.141592653589793
j. e : It returns the mathematical constant
import math print(math.e) Output : 2.718281828459045
Introduction to Python Modules
2. Statistics Module : Functions of statistics module are discussed below:
a. median( ) : This function returns the median of the data. Median means middle value. for example :
import statistics print(statistics.median([23, 45, 56, 67, 78])) Output : 56
b. mean( ) : This function returns the average of the data passed as an argument. for example
import statistics print(statistics.mean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) Output : 5.5
c. mode( ) : This function returns the mode ie a number with maximum number of occurrence. for example
import statistics print(statistics.mean([1, 2, 1, 4, 1, 2, 3, 2, 1, 5])) Output : 1
3. Random Module : Click here for notes and questions
PRACTICE EXERCISE
Q1. Write the output of the following :
import math a. print(math.floor(65.7)) b. print(math.ceil(-78.9)) c. print(math.sqrt(math.floor(64.56))) d. print(math.fabs(-90.78)) e. print(math.ceil(70.03)) f. print(math.sqrt(121)) g. print(math.pow(2,5)) h. print(math.pow(math.sqrt(16),math.ceil(2.96))) i. print(math.sqrt(math.ceil(35.21))) j. print(math.cos(0)) Solutions: a. 65 b. -78 c. 8.0 d. 90.78 e. 71 f. 11.0 g. 32.0 h. 64.0 i. 6.0 j. 1.0
Q2. Write the output of the following
import math a. print(math.ceil(35.7)) b. print(math.ceil(-98.9)) c. print(math.sqrt(math.ceil(63.01))) d. print(math.pow(9,math.sqrt(4))) e. print(math.pow(10,-2)) f. print(math.sqrt(81)) g. print(math.pow(4,-2)) h. print(math.pow(math.sqrt(100),math.ceil(-3.96))) i. print(math.sqrt(math.ceil(48.10))) j. print(math.tan(0)) Solutions : a. 36 b. -98 c. 8.0 d. 81.0 e. 0.01 f. 9.0 g. 0.0625 h. 0.001 i. 7.0 j. 0.0
Q3. Write the output of the following :
import statistics a. print(statistics.mean([1,2,3,4,5])) b. print(statistics.median([1,3,5,7,9])) c. print(statistics.median([1,3,5,7,9,11])) d. print(statistics.mode([1,3,3,2,2,3,4,5,2,3])) e. print(statistics.mode([23, 22, 25, 26, 23, 28, 23])) Solution : a. 3 b. 5 c. 6.0 d. 3 e. 23
Disclaimer : I tried to give you the correct Notes of ” Introduction to Python Modules ” , but if you feel that there is/are mistakes in the Notes of “ Introduction to Python Modules “ given above, you can directly contact me at csiplearninghub@gmail.com.
MCQ of Computer Science Chapter Wise
2. Flow of Control (Loop and Conditional statement)
3. 140+ MCQ on Introduction to Python
4. 120 MCQ on String in Python
7. 100+ MCQ on Flow of Control in Python
8. 60+ MCQ on Dictionary in Python
Important Links
100 Practice Questions on Python Fundamentals
120+ MySQL Practice Questions
90+ Practice Questions on List
50+ Output based Practice Questions
100 Practice Questions on String
70 Practice Questions on Loops
70 Practice Questions on if-else
Computer Science Syllabus 2021-2022.
Informatics Practices Syllabus 2021-2022
Class 12 Computer Science Chapter wise MCQ
The study material is good
It should have copy option
YOUR NOTES are excellent which makes teachers life very easy … this could be explained to them with great clarity… thanks