Skip to content
Computer ScienceCBSE Class 11 · Chapter 10

Introduction to Python Modules Class 11: import, math, random and statistics

Updated 14 October 2026 · Fact-checked

A module is a Python file of ready-made functions and constants that a program can import. In CBSE Class 11 Computer Science you learn the import and from statements and the listed functions of three modules: math (pi, e, sqrt, ceil, floor, pow, fabs, sin, cos, tan), random (random, randint, randrange) and statistics (mean, median, mode).

What Introduction to Python Modules is about

This chapter closes Unit II, Computational Thinking and Programming - 1, which carries 45 of the 70 theory marks in the school annual exam. Typical questions ask for the output of math functions, the possible outputs of a program that uses random numbers, the right import statement, and short programs using these functions.

The 2026-27 syllabus wording is: "Importing module using 'import <module>' and using from statement, importing math module (pi, e, sqrt(), ceil(), floor(), pow(), fabs(), sin(), cos(), tan()); random module (random(), randint(), randrange()), statistics module (mean(), median(), mode())". Only these functions are in scope.

NCERT teaches this in section 7.5 of its Functions chapter. Sections 7.1-7.4 of that chapter (writing your own functions, arguments, return values and scope) are Class XII content in the CBSE syllabus and are left out here. Every deterministic output below was produced by running the code in Python 3; random outputs are given as ranges.

Weightage: This chapter is part of Unit II, Computational Thinking and Programming - 1, which carries 45 of the 70 theory marks in the school annual exam together with Introduction to Problem Solving, Getting Started with Python, Flow of Control, Strings, Lists and Tuples and Dictionaries.Source: CBSE Computer Science (083) syllabus 2026-27, Class XI distribution of marks, Unit II Computational Thinking and Programming - 1

Key concepts

Modules and the import statement
NCERT section NCERT 7.5.1, 7.5.2 (A)

Python's standard library has built-in functions that are always available (print, len, abs, max, min, sum, round, int ...) and modules: .py files that group related functions and constants. A module's functions become available only after it is imported.

import modulename (several can be listed: import math, random) loads the whole module. Each function is then called with the module name and a dot: math.sqrt(25). Using sqrt(25) alone gives a NameError, and calling a module before importing it also gives a NameError.

Points to remember: module names are lowercase (Python is case-sensitive: import Math fails with ModuleNotFoundError); a module needs to be imported only once in a program; the import is usually written at the top. A program can also import a module under a shorter name with import statistics as st (then st.mean(...)).

Worked example

Find the error and correct it:
import math
print(sqrt(49) + math.pi)

Try it first, then show the solutionSolution
  1. After import math, functions must be called with the prefix math.
  2. sqrt alone is not defined: NameError.
  3. Correct: print(math.sqrt(49) + math.pi).

Answer: Use math.sqrt(49); the output is then 10.141592653589793.

The from statement
NCERT section NCERT 7.5.2 (B)

from modulename import name1, name2 brings only the named functions or constants into the program, and they are then used without the module prefix: from math import sqrt, pi; print(sqrt(16), pi). Only the names imported can be used; math.ceil would fail here because the name math itself was not imported.

from modulename import * brings in every public name. It saves typing but can clash with names in your own program (for example, a variable called e would be overwritten by math.e), so it is better avoided in large programs.

StatementHow to callWhat is available
import mathmath.sqrt(x)the whole module, through the prefix
from math import sqrtsqrt(x)only sqrt
from math import *sqrt(x), floor(x) ...all names, no prefix

Worked example

Which line fails, and why?
from math import ceil, pi
print(ceil(pi))
print(math.floor(pi))

Try it first, then show the solutionSolution
  1. ceil and pi were imported by name: ceil(3.14159...) = 4 prints fine.
  2. The name math was never imported, so math.floor raises NameError.

Answer: The third line fails with NameError; the second prints 4.

math constants and square root, power, absolute value
NCERT section NCERT 7.5.2, Table 7.2

After import math:

  • math.pi = 3.141592653589793; math.e = 2.718281828459045 (constants, no brackets).
  • math.sqrt(x): square root as a float: math.sqrt(49) = 7.0. A negative x raises ValueError.
  • math.pow(x, y): x raised to y, always a float: math.pow(2, 10) = 1024.0. (The built-in pow(2, 10) and 2 ** 10 give the int 1024.)
  • math.fabs(x): absolute value as a float: math.fabs(-7) = 7.0. (The built-in abs(-7) gives the int 7.)

Most math functions return floats, which is a common trap in output questions.

Worked example

Give the output:

import math
print(math.sqrt(81), math.pow(3, 3), math.fabs(-12), abs(-12))
print(round(math.pi, 3), round(math.e, 2))
Try it first, then show the solutionSolution
  1. sqrt gives 9.0; math.pow gives 27.0; fabs gives 12.0; built-in abs keeps the int 12.
  2. pi to 3 places is 3.142; e to 2 places is 2.72.

Answer: 9.0 27.0 12.0 12
3.142 2.72

ceil() and floor()
NCERT section NCERT Table 7.2
  • math.ceil(x): the smallest integer greater than or equal to x (rounds up towards plus infinity).
  • math.floor(x): the largest integer less than or equal to x (rounds down towards minus infinity).

Both return an int in Python 3. Careful with negatives: ceil(-4.2) = -4 (up is towards zero here) and floor(-4.2) = -5. For a whole number both return the same value: ceil(6) = floor(6) = 6.

These differ from int(), which simply cuts off the fraction (int(-4.7) = -4), and from round(), which goes to the nearest integer.

Worked example

Give math.ceil and math.floor of 7.01, -7.01, 7.99 and -7.0, and compare with int().

Try it first, then show the solutionSolution
  1. 7.01: ceil 8, floor 7. -7.01: ceil -7, floor -8.
  2. 7.99: ceil 8, floor 7. -7.0: both -7.
  3. int() cuts off: int(7.99) = 7, int(-7.01) = -7.

Answer: ceil: 8, -7, 8, -7; floor: 7, -8, 7, -7.

sin(), cos() and tan()
NCERT section NCERT Table 7.2

math.sin(x), math.cos(x) and math.tan(x) take the angle x in radians, not degrees. To use degrees, convert first: radians = degrees × π / 180 (for example, 30° = math.pi / 6). The results are floats and may show tiny rounding errors, such as math.sin(math.pi) = 1.2246467991473532e-16 instead of exactly 0, so round() them when displaying.

Worked example

Find sin 30°, cos 60° and tan 45° using the math module, rounded to 2 decimal places.

Try it first, then show the solutionSolution
  1. import math
    for deg, f in [(30, math.sin), (60, math.cos), (45, math.tan)]:
        rad = deg * math.pi / 180
        print(deg, round(f(rad), 2))
  2. Convert each angle to radians before calling the function.

Answer: 30 0.5
60 0.5
45 1.0

The random module
NCERT section NCERT Table 7.3

After import random:

  • random.random(): a random float with 0.0 ≤ value < 1.0 (it can be 0.0 but never 1.0).
  • random.randint(a, b): a random integer with a ≤ value ≤ b: both ends included.
  • random.randrange(stop): a random integer from 0 to stop - 1.
  • random.randrange(start, stop[, step]): a random integer from the values that range(start, stop, step) would give, so stop is excluded: randrange(1, 10, 2) gives one of 1, 3, 5, 7, 9.

Because the output changes every run, exam questions ask for the possible outputs or the minimum and maximum values a variable can take. To scale random(): int(random.random() * 6) + 1 gives an integer from 1 to 6, like a dice.

Worked example

Give the smallest and largest possible values of: (a) random.randint(3, 8) (b) random.randrange(3, 8) (c) random.randrange(10, 50, 10) (d) int(random.random() * 5) + 2

Try it first, then show the solutionSolution
  1. (a) both ends included: 3 to 8.
  2. (b) stop excluded: 3 to 7.
  3. (c) values 10, 20, 30, 40: 10 to 40.
  4. (d) random() * 5 is 0 to just below 5; int gives 0 to 4; +2 gives 2 to 6.

Answer: (a) 3 and 8 (b) 3 and 7 (c) 10 and 40 (d) 2 and 6

The statistics module
NCERT section NCERT Table 7.4

After import statistics:

  • statistics.mean(data): the arithmetic mean (sum ÷ count).
  • statistics.median(data): the middle value of the sorted data; for an even number of values, the average of the two middle values (so the result may be a float not in the data).
  • statistics.mode(data): the most frequent value. It works on numbers and on non-numeric data such as strings. If several values tie for most frequent, Python 3.8 and later return the first of them met in the data.

The data may be a list or a tuple; an empty sequence raises an error (StatisticsError).

Worked example

For marks = [56, 78, 91, 78, 64, 85], find the mean, median and mode with the statistics module.

Try it first, then show the solutionSolution
  1. import statistics as st
    marks = [56, 78, 91, 78, 64, 85]
    print(round(st.mean(marks), 2), st.median(marks), st.mode(marks))
  2. Mean = 452 / 6 = 75.33. Sorted: 56, 64, 78, 78, 85, 91; middle two 78 and 78 → median 78.0. 78 occurs twice: mode 78.

Answer: 75.33 78.0 78

Introduction to Python Modules in real life

Each concept, met in an everyday situation. Read the situation, answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

randint for an OTP

Suppose a school app sends a 4-digit one-time password to a parent's phone.

Which call makes sure the OTP always has exactly 4 digits (no leading zero), and what are its extremes?

Try it first, then show the solutionSolution
  1. Four-digit numbers run from 1000 to 9999.
  2. random.randint(1000, 9999) includes both ends.

Answer: random.randint(1000, 9999); smallest 1000, largest 9999.

Memory hook: randint includes both ends; randrange leaves out the stop.

ceil in practical counting

Imagine 130 students going on a trip in vans that seat 12 each.

How many vans are needed, and which math function gives it directly?

Try it first, then show the solutionSolution
  1. 130 / 12 = 10.83, but a part-full van is still a van.
  2. math.ceil(130 / 12) = 11.

Answer: 11 vans, using math.ceil.

Memory hook: Anything left over needs one more: round up with ceil.

median versus mean

Suppose five friends' monthly pocket money is ₹500, ₹600, ₹550, ₹650 and ₹5000.

Find the mean and the median. Which better describes a typical friend?

Try it first, then show the solutionSolution
  1. Mean = 7300 / 5 = 1460.
  2. Sorted: 500, 550, 600, 650, 5000 → median 600.

Answer: Mean ₹1460, median ₹600; the median, because one large value pulls the mean up.

Memory hook: Median ignores one extreme value; mean does not.

Topics in this chapter

Introduction to Python Modules: syntax and key facts

Import forms
  1. 1.import m
  2. 2.m.f(); from m import f
  3. 3.f(); from m import *
  4. 4.all names without prefix
import m as x → x.f().
math constants
math.pi ≈ 3.141592653589793; math.e ≈ 2.718281828459045
No brackets: they are values, not functions.
Rounding functions
ceil(x): smallest int ≥ x; floor(x): largest int ≤ x
ceil(-2.5) = -2; floor(-2.5) = -3.
Float results
math.sqrt, math.pow, math.fabs return floats
abs() and pow()/** on ints return ints.
Random ranges
random(): [0.0, 1.0); randint(a, b): a..b; randrange(a, b): a..b-1
randrange(a, b, s) follows range(a, b, s).
Statistics
mean = sum/count; median = middle of sorted data (average of two middles if even); mode = most frequent
mode returns the first of tied values (Python 3.8+).

How the school exam asks Introduction to Python Modules

The question patterns to expect, and the method that works for each.

Question typeHow it is askedMethod
Output of math functionsprint(math.ceil(-3.2), math.floor(3.9), math.sqrt(16), math.pow(2, 3)) (1-2 marks).Watch the type: sqrt, pow and fabs give floats; ceil and floor give ints; negatives round towards the stated direction.
Possible outputs with randomA program with randint or randrange; choose which listed outputs are possible and give the minimum and maximum values (2-3 marks).Write the exact range of each random call; check every option against it, especially end points.
Which module / which importName the module needed for a function, or the import statement that makes a line work (1 mark).math for sqrt, ceil, floor, pow, fabs, trig; random for random numbers; statistics for mean, median, mode.
Correct the codeFix missing imports, wrong prefixes or wrong case (2 marks).Match the call style to the import style; lowercase module names.
Short programsArea of a circle, hypotenuse, dice simulation, OTP generation, mean/median/mode of input data (2-3 marks).Import first, read and convert input, call the functions, print with labels.

Tricks to remember Introduction to Python Modules

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Dot after import, no dot after from

    import math → math.sqrt; from math import sqrt → sqrt.

    Use it when: Choosing the right call style.

  • Ceiling is above, floor is below

    Picture a number line: ceil goes up to the ceiling, floor goes down to the floor, even for negatives.

    Use it when: ceil/floor of negative numbers.

  • randINT is INclusive

    randint includes both ends; randrange behaves like range and stops short.

    Use it when: Possible-output questions.

  • math gives floats

    sqrt, pow, fabs: the 'f' in fabs reminds you of float.

    Use it when: Output format of math functions.

  • Trig wants radians

    Multiply degrees by pi/180 before sin, cos or tan.

    Use it when: Trigonometric outputs.

Introduction to Python Modules: exam-level solved questions

Ordered from exam level to HOTS. Cover the solution, write your own, then compare step by step.

Example 1: Mixed math outputs · 2 marks · Short answerExam level

Give the output:

import math
print(math.ceil(-2.5), math.floor(-2.5), math.ceil(2.5), math.floor(2.5))
print(math.pow(4, 0.5), math.sqrt(2.25), math.fabs(-0.5))
Try it first, then show the solutionSolution
  1. ceil(-2.5) = -2, floor(-2.5) = -3, ceil(2.5) = 3, floor(2.5) = 2.
  2. 4 ** 0.5 = 2.0; sqrt(2.25) = 1.5; fabs(-0.5) = 0.5.

Answer: -2 -3 3 2
2.0 1.5 0.5

Example 2: Possible outputs with randrange · 3 marks · Short answerHarder exam level

import random
COLORS = ['Red', 'Blue', 'Green', 'Pink']
start = random.randrange(0, 2)
end = random.randrange(2, 4)
for i in range(start, end + 1):
print(COLORS[i], end='#')
Which are possible? (i) Red#Blue#Green# (ii) Blue#Green#Pink# (iii) Green#Pink# (iv) Red#Blue#Green#Pink#. Give the minimum and maximum of start and end.

Try it first, then show the solutionSolution
  1. start is 0 or 1; end is 2 or 3.
  2. Printed from index start to end inclusive (range goes to end + 1).
  3. (i) start 0, end 2: possible. (ii) start 1, end 3: possible. (iii) needs start 2: impossible. (iv) start 0, end 3: possible.

Answer: (i), (ii) and (iv) are possible; start: 0 to 1, end: 2 to 3.

Example 3: Dice simulation · 3 marks · Short answerHarder exam level

Write a program that rolls two dice 1000 times and prints how many times the total is 7, and check that every total lies between 2 and 12.

Try it first, then show the solutionSolution
  1. import random
    sevens = 0
    low, high = 12, 2
    for i in range(1000):
        t = random.randint(1, 6) + random.randint(1, 6)
        if t == 7:
            sevens += 1
        low = min(low, t)
        high = max(high, t)
    print(sevens, low, high)
  2. 6 of the 36 equally likely pairs total 7, so sevens is usually near 1000 × 6/36 ≈ 167. low and high come out as 2 and 12 in almost every run.

Answer: For example 172 2 12 in one run; the count of sevens changes from run to run (about 167 on average), while 2 and 12 appear in almost every run.

Example 4: Statistics of a tuple · 2 marks · Short answerExam level

Give the output:

from statistics import mean, median, mode
t = (12, 7, 3, 7, 10, 3, 7, 15)
print(mean(t), median(t), mode(t))
Try it first, then show the solutionSolution
  1. Sum 64 / 8 = 8 (mean of ints gives an int when exact).
  2. Sorted 3, 3, 7, 7, 7, 10, 12, 15: middle two 7 and 7 → 7.0.
  3. 7 occurs three times: mode 7.

Answer: 8 7.0 7

Example 5: Hypotenuse and rounding up · 3 marks · Short answerHOTS / competency

Suppose a cable must run diagonally across a rectangular hall 20 m by 7 m and cable is sold only in whole metres. Write a program to find the diagonal (2 decimals) and the number of metres to buy, and say why ceil is right and round() is not.

Try it first, then show the solutionSolution
  1. import math
    d = math.sqrt(math.pow(20, 2) + math.pow(7, 2))
    print(round(d, 2), math.ceil(d), round(d))
  2. √(400 + 49) = √449 ≈ 21.19 m.
  3. ceil gives 22 m, enough cable. round() gives 21 m, which is 0.19 m too short; ceil always rounds up.

Answer: 21.19 22 21: buy 22 m (math.ceil), not 21 m.

Example 6: Fix the imports · 2 marks · Short answerExam level

Correct the code so that it runs:
import Math, Random
x = random.randint(1, 9)
print(Math.sqrt(x), floor(2.7))

Try it first, then show the solutionSolution
  1. Module names are lowercase: import math, random.
  2. floor needs the prefix: math.floor(2.7).
  3. import math, random
    x = random.randint(1, 9)
    print(math.sqrt(x), math.floor(2.7))

Answer: Corrected code as above. The first value depends on x (for example 2.0 when x is 4); the second is always 2.

Example 7: Median of an even-length list · 2 marks · Short answerHarder exam level

Without running, give statistics.median([40, 10, 30, 20]) and statistics.mode(['b', 'a', 'b', 'a', 'c']), then check by program.

Try it first, then show the solutionSolution
  1. Sorted 10, 20, 30, 40: average of 20 and 30 = 25.0.
  2. 'b' and 'a' both occur twice; the first met in the data is 'b'.
  3. import statistics
    print(statistics.median([40, 10, 30, 20]), statistics.mode(['b', 'a', 'b', 'a', 'c']))

Answer: 25.0 b

Example 8: Random float to a range · 2 marks · Short answerHOTS / competency

Using only random.random() and math functions, write an expression for a random integer from 5 to 15 inclusive, and prove its range.

Try it first, then show the solutionSolution
  1. random() is in [0, 1); × 11 gives [0, 11).
  2. math.floor gives 0 to 10; adding 5 gives 5 to 15.
  3. Expression: math.floor(random.random() * 11) + 5.

Answer: math.floor(random.random() * 11) + 5, which gives every integer from 5 to 15.

Case-study practice (Section E style)

Read the situation, then answer every part. In a school exam set on the board pattern this is a 4-mark question split into 1 + 1 + 1 + 1 marks.

Quiz app question picker

Suppose a class quiz app stores 20 questions in a list Q (indices 0 to 19). For each round it picks a random question, gives a random bonus of 1 to 3 points, and at the end reports the mean, median and mode of the scores of all players.

  1. (i) · 1 mark

    Write the call that picks a valid random index, and one that picks the bonus.

    Answer

    Answer: random.randrange(20) (or randint(0, 19)) for the index; random.randint(1, 3) for the bonus.

    randrange(20) gives 0-19; randint includes 3.

  2. (ii) · 1 mark

    A student writes random.randint(0, 20) for the index. What can go wrong?

    Answer

    Answer: It can return 20, and Q[20] raises IndexError because the last index is 19.

    randint includes its upper end.

  3. (iii) · 1 mark

    The scores are [14, 18, 14, 20, 11, 17]. Give the mean (2 decimals), median and mode using statistics.

    Answer

    Answer: Mean 94 / 6 = 15.67; sorted 11, 14, 14, 17, 18, 20 → median (14 + 17)/2 = 15.5; mode 14.

    Even number of values: average of the two middle ones.

  4. (iv) · 1 mark

    Which import line lets the program write mean(scores) and randint(1, 3) without prefixes?

    Answer

    Answer: from statistics import mean
    from random import randint

    from ... import brings names in without the module prefix.

Where the marks are

How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.

Differentiate between import math and from math import sqrt with an example. [2 marks] (2 marks)

StepMarks
import math loads the whole module; functions are called with the prefix, e.g. math.sqrt(25)1
from math import sqrt brings in only sqrt, called without a prefix, e.g. sqrt(25); other math names are not available1

What are the possible outputs of the following, and what are the minimum and maximum values of x?
import random
L = ['A', 'B', 'C', 'D', 'E']
x = random.randint(1, 3)
for i in range(x, 5):
print(L[i], end='')
Options: (i) BCDE (ii) ABCDE (iii) DE (iv) CDE [3 marks]
(3 marks)

StepMarks
x is 1, 2 or 3 (randint includes both ends): minimum 1, maximum 31
x = 1 prints BCDE, x = 2 prints CDE, x = 3 prints DE1
Possible: (i), (iii), (iv); (ii) is impossible because x is never 01

Write a program that reads the radius of a circular park, then prints its area and circumference rounded to 2 decimal places, the number of whole 10 m² grass patches needed to cover it (rounded up), and the length of the longest straight path inside it. [5 marks] (5 marks)

StepMarks
import math and read r with float(input())1
area = math.pi * r ** 2 (or math.pow(r, 2)); circumference = 2 * math.pi * r1.5
Printing both with round(..., 2)0.5
Patches = math.ceil(area / 10)1
Longest path = diameter = 2 * r, printed1

Where students lose marks in Introduction to Python Modules

The slips examiners see most often, and what to write instead.

  • Mistake: Writing math.sqrt(16) = 4

    sqrt returns a float.

    Wrong working

    print(math.sqrt(16)) → 4

    Right working

    print(math.sqrt(16)) → 4.0

    Do this: Show .0 for sqrt, pow and fabs results.

    Usually costs: Half to 1 mark

  • Mistake: Rounding negatives the wrong way with ceil and floor

    ceil goes towards plus infinity, floor towards minus infinity.

    Wrong working

    math.floor(-3.2) = -3

    Right working

    math.floor(-3.2) = -4; math.ceil(-3.2) = -3

    Do this: Mark the number on a number line.

    Usually costs: 1 mark

  • Mistake: Including stop in randrange

    randrange(a, b) can never return b.

    Wrong working

    randrange(1, 6) can give 6

    Right working

    randrange(1, 6) gives 1 to 5; randint(1, 6) gives 1 to 6

    Do this: Treat randrange like range().

    Usually costs: 1 mark

  • Mistake: Calling with the prefix after a from import

    from math import sqrt does not create the name math.

    Wrong working

    from math import sqrt
    print(math.sqrt(9))

    Right working

    from math import sqrt
    print(sqrt(9))

    Do this: Match the call to the import form.

    Usually costs: 1 mark

  • Mistake: Passing degrees to sin()

    math.sin expects radians.

    Wrong working

    math.sin(30) → 0.5

    Right working

    math.sin(30) ≈ -0.988 (30 radians); math.sin(math.pi / 6) ≈ 0.5

    Do this: Convert with degrees × pi / 180.

    Usually costs: 1 mark

NCERT exercise guide: Introduction to Python Modules

What each exercise in the NCERT chapter tests, how to approach it, and a question of the same kind solved step by step. Questions here are written in our own words.

NCERT Chapter 7 exercise: questions on modules (section 7.5 only)

What it tests: Using math, random and statistics functions, writing import statements and predicting outputs.

How to approach it: Answer only the module questions; skip those that ask you to write user-defined functions (Class XII content). Check float versus int results and the range of random calls.

Same kind as the 'use the math module to compute' questions

A ladder 13 m long reaches a window 12 m high. Use the math module to find how far the foot of the ladder is from the wall, and check whether the ladder makes an angle of about 67.4° with the ground (sin of that angle should be 12/13).

Show the solutionSolution
  1. import math
    ladder, h = 13, 12
    base = math.sqrt(math.pow(ladder, 2) - math.pow(h, 2))
    print(base)
    angle = 67.4 * math.pi / 180
    print(round(math.sin(angle), 3), round(h / ladder, 3))
  2. base = √(169 - 144) = √25 = 5.0 m (a float, because sqrt and pow return floats).
  3. The angle is changed to radians before sin(); both values round to 0.923, so the angle is about 67.4°.

Answer: 5.0
0.923 0.923

Practice questions on Introduction to Python Modules

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQExam level

    Which module must be imported to use median()? (a) math (b) random (c) statistics (d) none

    Hint

    Average-type functions.

    Answer

    (c) statistics

  2. Q2 · 1 mark · Very short answerHarder exam level

    Give the output: import math; print(math.floor(-0.5), math.ceil(-0.5), math.sqrt(0.25))

    Hint

    Negative numbers round towards the stated side.

    Answer

    -1 0 0.5 (ceil(-0.5) is shown as 0)

  3. Q3 · 1 mark · Very short answerHarder exam level

    What is the smallest and largest value of y? import random; y = random.randrange(5, 25, 5) * 2

    Hint

    randrange gives 5, 10, 15, 20.

    Answer

    Smallest 10, largest 40.

  4. Q4 · 3 marks · Short answerExam level

    Write a program to input the three sides of a triangle and print its area using Heron's formula, s = (a + b + c)/2 and area = √(s(s-a)(s-b)(s-c)).

    Hint

    math.sqrt.

    Answer

    import math
    a = float(input()); b = float(input()); c = float(input())
    s = (a + b + c) / 2
    print(math.sqrt(s * (s - a) * (s - b) * (s - c)))
    (For 3, 4, 5 the area is 6.0.)

  5. Q5 · 1 mark · MCQHarder exam level

    Assertion (A): math.pow(2, 3) and 2 ** 3 print the same thing. Reason (R): math.pow always returns a float. Choose (a) both true, R explains A (b) both true, R does not explain A (c) A false, R true (d) both false.

    Hint

    8.0 versus 8.

    Answer

    (c) A is false (8.0 and 8 are printed); R is true.

  6. Q6 · 2 marks · Short answerHarder exam level

    Give the output:

    from math import ceil, floor
    L = [2.3, -2.3, 5.0]
    print([ceil(x) for x in L], [floor(x) for x in L])
    Hint

    A list built by a loop expression.

    Answer

    [3, -2, 5] [2, -3, 5]

  7. Q7 · 2 marks · Short answerHOTS / competency

    import random
    N = random.randint(2, 4)
    for i in range(1, N):
    print(i * '*')
    Which outputs are possible? (i) * then (ii) * then then * (iii) only * (iv) * then then * then **

    Hint

    N is 2, 3 or 4; range(1, N) stops before N.

    Answer

    N = 2 → (iii); N = 3 → (i); N = 4 → (ii). (iv) is impossible.

  8. Q8 · 2 marks · Short answerExam level

    Why does print(math.sin(90)) not print 1.0? Correct it.

    Hint

    Units.

    Answer

    sin takes radians; 90 radians is not 90°. Use math.sin(math.radians(90)) or math.sin(90 * math.pi / 180), which gives 1.0.

  9. Q9 · 3 marks · Short answerHOTS / competency

    Write a program that generates 5 random marks between 30 and 100 (inclusive) into a list and prints the list with its mean and the highest mark rounded up to the next multiple of 10.

    Hint

    math.ceil(x / 10) * 10.

    Answer

    import random, math, statistics
    m = []
    for i in range(5):
    m.append(random.randint(30, 100))
    print(m, statistics.mean(m), math.ceil(max(m) / 10) * 10)

  10. Q10 · 2 marks · Short answerHarder exam level

    Give the output: import statistics as s; print(s.mean([1, 2, 3, 4]), s.median([5, 1, 3]), s.mode('hello'))

    Hint

    A string is a sequence of characters.

    Answer

    2.5 3 l

  11. Q11 · 2 marks · Short answerHOTS / competency

    A program has 'from math import *' and later the line e = 5. What happens to the name e (which the import set to Euler's number) in that program? Why is this a risk?

    Hint

    Name clash.

    Answer

    The name e (which from math import * set to 2.718...) is replaced by 5; any later use of e meaning Euler's number is wrong. Importing everything without a prefix can clash with your own names.

  12. Q12 · 1 mark · Very short answerExam level

    Name the function: (a) returns the absolute value as a float (b) gives the largest integer not greater than x (c) random float below 1

    Hint

    All three are in the syllabus list.

    Answer

    (a) math.fabs (b) math.floor (c) random.random

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Which module has the function median()?Show answer

    statistics.

  2. 2.What is math.ceil(4.0001)?Show answer

    5.

  3. 3.What is the range of random.random()?Show answer

    0.0 up to but not including 1.0.

  4. 4.What does math.fabs(-3) return?Show answer

    3.0.

  5. 5.Can random.randint(1, 10) return 10?Show answer

    Yes, both ends are included.

  6. 6.How do you import only pi from math?Show answer

    from math import pi.

  7. 7.What does statistics.median([4, 1, 3, 2]) return?Show answer

    2.5.

Best order to study it

  1. What a module is; built-in functions versus module functions.
  2. import, import ... as, from ... import, from ... import *.
  3. math: pi, e, sqrt, pow, fabs; ceil and floor with negatives; sin, cos, tan with radians.
  4. random: random, randint, randrange and the possible-output question type.
  5. statistics: mean, median, mode.
  6. Short programs combining modules.

Quick revision checklist

The night before the exam: can you do each of these without looking?

  • Module: a file of related functions; must be imported before use.
  • import math → math.sqrt(x); from math import sqrt → sqrt(x).
  • math.pi, math.e are constants.
  • sqrt, pow, fabs return floats; ceil (up) and floor (down) return ints.
  • sin, cos, tan take radians.
  • random() in [0, 1); randint(a, b) includes b; randrange(a, b) excludes b.
  • mean, median (average of the middle two for even counts), mode (first of ties).

Introduction to Python Modules: frequently asked questions

Are user-defined functions part of this chapter?

No. The 2026-27 Class XI syllabus covers only importing modules and the listed functions of math, random and statistics. Writing your own functions, parameters, return values and scope (NCERT 7.1-7.4) are taught in Class XII.

Are other math functions such as factorial(), gcd() or fmod() needed?

NCERT lists them in Table 7.2, but the syllabus limits the math module to pi, e, sqrt(), ceil(), floor(), pow(), fabs(), sin(), cos() and tan(). Knowing the others does no harm, but questions are set on the listed ones.

What is the difference between pow() and math.pow()?

Both raise x to the power y. The built-in pow(2, 3) returns the int 8 (and accepts a third argument for a remainder), while math.pow(2, 3) always returns the float 8.0.

Why do I get a different answer every time I run a random program?

That is the purpose of the random module. In exam questions you therefore give the possible outputs or the range of values, not one fixed output.

What happens if statistics.mode() finds two values with the same highest count?

In Python 3.8 and later it returns the first of the tied values to appear in the data, e.g. mode([3, 5, 3, 5]) is 3. Older versions raised an error in this case.

Based on: CBSE Computer Science (083) syllabus, Class XI 2026-27; NCERT Computer Science, Textbook for Class XI, Chapter 7 Functions (Reprint 2026-27)