Computer Science

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 import and from statements.
  • Work with the Python math module.
  • 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 math module before using its functions.
  • Writing sqrt() instead of math.sqrt() after using import math.
  • Using degrees instead of radians with sin(), cos(), and tan().
  • Confusing ceil() and floor().
  • Using pow() without understanding that it returns a floating-point value.

CBSE Important Programs

  1. Find the square root of a number.
  2. Calculate the area of a circle using math.pi.
  3. Round numbers using ceil().
  4. Round numbers using floor().
  5. Calculate powers using pow().
  6. Find the absolute value of a number.
  7. Display the values of sin(), cos(), and tan().
  8. Import selected functions using the from statement.

Viva Questions

  1. What is a Python module?
  2. What is the advantage of using modules?
  3. Differentiate between import and from.
  4. What is the purpose of the math module?
  5. What does math.pi return?
  6. What is the difference between ceil() and floor()?
  7. What is the purpose of fabs()?
  8. Why are angles passed in radians to trigonometric functions?
  9. Which function calculates the square root?
  10. Which function calculates powers?

Exam Tips

  • Always import the module before using its functions.
  • Remember that ceil() rounds upward and floor() rounds downward.
  • sqrt() returns a floating-point value.
  • Trigonometric functions use radians, not degrees.
  • Use the from statement when only specific functions are required.

Quick Revision

  • A module is a reusable Python file.
  • import imports the complete module.
  • from imports selected functions.
  • math.pi returns π.
  • 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(), and math.tan() work with angles in radians.