Python Modules (Part 1) | import, from Statement, Math Module & Functions | CBSE Class 11 Computer Science
Class 11 · Computer Science
Python Modules (Part 1)
Python provides a large collection of built-in modules that contain pre-written functions and constants. Instead of writing code from scratch, programmers can import these modules and use their functions directly. Modules make programs shorter, reusable, and easier to maintain.
Learning Objectives
- Understand the concept of modules.
- Import built-in modules.
- Use the
importandfromstatements. - Work with the Python
mathmodule. - Use common mathematical functions.
What is a Module?
A module is a Python file that contains functions, variables, and constants which can be reused in other Python programs.
Instead of writing the same code repeatedly, Python programmers import modules whenever required.
Advantages of Modules
- Promote code reusability.
- Reduce program size.
- Improve readability.
- Save development time.
- Provide tested and reliable functions.
Importing a Module
Syntax
import module_name
Example
import math
print(math.sqrt(25))
Output
5.0
Using the from Statement
The from statement imports specific functions or constants from a module. After importing, the module name is not required.
Syntax
from module_name import function_name
Example
from math import sqrt
print(sqrt(49))
Output
7.0
Difference between import and from
| import | from |
|---|---|
| Imports the complete module. | Imports selected functions or constants. |
| Module name is required. | Module name is not required. |
math.sqrt() |
sqrt() |
Math Module
The math module provides mathematical constants and functions for performing scientific calculations.
import math
1. math.pi
Returns the value of π (Pi).
import math
print(math.pi)
Output
3.141592653589793
2. math.e
Returns the value of Euler's number (e).
import math
print(math.e)
Output
2.718281828459045
3. math.sqrt()
Returns the square root of a number.
Syntax
math.sqrt(number)
Example
import math
print(math.sqrt(81))
Output
9.0
4. math.ceil()
Returns the smallest integer greater than or equal to the given number.
import math
print(math.ceil(5.2))
print(math.ceil(7.9))
Output
6
8
5. math.floor()
Returns the largest integer less than or equal to the given number.
import math
print(math.floor(5.9))
print(math.floor(7.2))
Output
5
7
6. math.pow()
Returns the value of one number raised to the power of another.
Syntax
math.pow(x,y)
Example
import math
print(math.pow(2,5))
Output
32.0
7. math.fabs()
Returns the absolute (positive) value of a number as a floating-point value.
import math
print(math.fabs(-25))
print(math.fabs(18))
Output
25.0
18.0
8. math.sin()
Returns the sine of an angle given in radians.
import math
print(math.sin(0))
Output
0.0
9. math.cos()
Returns the cosine of an angle given in radians.
import math
print(math.cos(0))
Output
1.0
10. math.tan()
Returns the tangent of an angle given in radians.
import math
print(math.tan(0))
Output
0.0
Summary Table
| Function / Constant | Purpose |
|---|---|
math.pi |
Returns the value of π. |
math.e |
Returns Euler's number. |
math.sqrt() |
Returns the square root. |
math.ceil() |
Rounds up to the nearest integer. |
math.floor() |
Rounds down to the nearest integer. |
math.pow() |
Calculates powers. |
math.fabs() |
Returns the absolute value. |
math.sin() |
Returns the sine value. |
math.cos() |
Returns the cosine value. |
math.tan() |
Returns the tangent value. |
Real-Life Applications
- Scientific calculations.
- Engineering computations.
- Game development.
- Graphics and animations.
- Financial calculations.
- Physics simulations.
Common Programming Mistakes
- Forgetting to import the
mathmodule before using its functions. - Writing
sqrt()instead ofmath.sqrt()after usingimport math. - Using degrees instead of radians with
sin(),cos(), andtan(). - Confusing
ceil()andfloor(). - Using
pow()without understanding that it returns a floating-point value.
CBSE Important Programs
- Find the square root of a number.
- Calculate the area of a circle using
math.pi. - Round numbers using
ceil(). - Round numbers using
floor(). - Calculate powers using
pow(). - Find the absolute value of a number.
- Display the values of
sin(),cos(), andtan(). - Import selected functions using the
fromstatement.
Viva Questions
- What is a Python module?
- What is the advantage of using modules?
- Differentiate between
importandfrom. - What is the purpose of the
mathmodule? - What does
math.pireturn? - What is the difference between
ceil()andfloor()? - What is the purpose of
fabs()? - Why are angles passed in radians to trigonometric functions?
- Which function calculates the square root?
- Which function calculates powers?
Exam Tips
- Always import the module before using its functions.
- Remember that
ceil()rounds upward andfloor()rounds downward. sqrt()returns a floating-point value.- Trigonometric functions use radians, not degrees.
- Use the
fromstatement when only specific functions are required.
Quick Revision
- A module is a reusable Python file.
importimports the complete module.fromimports selected functions.math.pireturns π.math.sqrt()finds the square root.math.ceil()rounds upward.math.floor()rounds downward.math.pow()calculates powers.math.fabs()returns the absolute value.math.sin(),math.cos(), andmath.tan()work with angles in radians.