Circle of Learning Logo CIRCLE OF LEARNING Excellence in Education
CBSE Class 11 · Computer Science Chapter 8 Session 2026-27

Python Modules & Functions Formulas & Cheat Sheet

Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.

Core Concepts & Theoretical Foundations

  • What is a Module: A Python file (.py) containing function definitions, classes, and variables that can be imported and reused in other programs.
  • Importing Modules: 1. import module_name (access via module.func()); 2. from module_name import func1, func2 (direct access via func1()); 3. from module_name import * (imports all names; discouraged due to namespace pollution); 4. import module as alias (renames module).
  • The math Module: Standard mathematical functions: math.sqrt(x), math.ceil(x) (smallest integer >= x), math.floor(x) (largest integer <= x), math.pow(x, y) (returns float), math.fabs(x) (absolute float), math.sin(x), math.cos(x), math.pi, math.e, math.gcd(a, b).
  • The random Module: Random number generation: random.random() (float in [0.0, 1.0)); random.randint(a, b) (integer in [a, b] inclusive of BOTH endpoints!); random.randrange(start, stop, step) (integer in [start, stop) exclusive of stop!); random.choice(seq) (random element from sequence).
  • The statistics Module: statistics.mean(data), statistics.median(data), statistics.mode(data).
  • Built-in Functions: abs(), divmod(a, b) (returns (a//b, a%b)), max(), min(), sum(), pow(), round(x, n), len(), type(), id(), input(), print().
  • User-Defined Functions: Defined using def function_name(parameters):; return statement sends values back to caller (functions without return implicitly return None); Default parameters must follow non-default parameters.

Chapter Formulas & Equations (10 entries)

# Topic / Formula Name Mathematical Form & Equation Conditions, Meaning & Board Tips
1 Random Module Range Differences
{random()∈[0.0,1.0)randint(a,b)∈{a,a+1,…,b}(b included)randrange(a,b)∈{a,a+1,…,b−1}(b excluded)\begin{cases} \text{random}() \in [0.0, 1.0) \\ \text{randint}(a, b) \in \{a, a+1, \dots, b\} \quad (b \text{ included}) \\ \text{randrange}(a, b) \in \{a, a+1, \dots, b-1\} \quad (b \text{ excluded}) \end{cases}

Critical board exam distinction: randint includes the upper limit, while randrange excludes the upper limit.

2 Math Ceil and Floor Integer Bounds
⌊x⌋≤x≤⌈x⌉[math.floor(x)≤x≤math.ceil(x)]\lfloor x \rfloor \le x \le \lceil x \rceil \quad [\text{math.floor(x)} \le x \le \text{math.ceil(x)}]

ceil always rounds up towards positive infinity; floor always rounds down towards negative infinity.

3 The divmod() Tuple Return Formula
divmod(a,b)=(q,r)  ⟺  a=b⋅q+r\text{divmod}(a, b) = (q, r) \iff a = b \cdot q + r

Calculates integer quotient and modulus simultaneously in a single efficient CPU operation.

4 Default Parameter Rule Syntax
Parameters:[Positional / Non-default]→[Default / Optional]\text{Parameters}: [\text{Positional / Non-default}] \rightarrow [\text{Default / Optional}]

In function definitions, all parameters with default values must appear after all parameters without default values.

5 Statistics Module Measures of Central Tendency
xˉ=1N∑xi,Median=X⌊(N+1)/2⌋,Mode=arg⁡max⁡(freq)\bar{x} = \frac{1}{N} \sum x_i, \quad \text{Median} = X_{\lfloor (N+1)/2 \rfloor}, \quad \text{Mode} = \arg\max(\text{freq})

Standard descriptive statistical functions provided by Python's built-in statistics library.

6 Math pow() vs Built-in pow() vs **
math.pow(x,y)∈Rvspow(x,y,m)=xy(modm)\text{math.pow}(x, y) \in \mathbb{R} \quad \text{vs} \quad \text{pow}(x, y, m) = x^y \pmod m

Built-in pow() supports 3-argument modular exponentiation, whereas math.pow() always converts outputs to floats.

7 Random Choice Uniform Distribution
P(xi)=1N(∀xi∈seq)P(x_i) = \frac{1}{N} \quad (\forall x_i \in \text{seq})

Selects a random element uniformly from any non-empty sequence (list, tuple, or string).

8 Greatest Common Divisor (math.gcd)
gcd⁡(a,b)=gcd⁡(b,a(modb))[Euclidean Algorithm]\gcd(a, b) = \gcd(b, a \pmod b) \quad [\text{Euclidean Algorithm}]

Standard mathematical function to compute HCF / GCD of two integers.

9 Function Variable Scope (LEGB Rule)
Scope Search:Local⟶Enclosing⟶Global⟶Built-in\text{Scope Search}: \text{Local} \longrightarrow \text{Enclosing} \longrightarrow \text{Global} \longrightarrow \text{Built-in}

Python's namespace lookup order to resolve variable identifiers inside functions.

10 Round Function Half-to-Even Rule
round(x)  ⟹  Banker’s Rounding (Rounds half to nearest even integer)\text{round}(x) \implies \text{Banker's Rounding (Rounds half to nearest even integer)}

Python 3 uses banker's rounding where numbers exactly halfway between integers round to the nearest even number.

CBSE Board Exam Tips & Frequent Pitfalls

  • In questions predicting the range of random output, remember random.randint(1, 6) can generate both 1 and 6 (inclusive), whereas random.randrange(1, 6) generates 1, 2, 3, 4, 5 (6 is excluded).
  • Remember: math.ceil(-4.2) is -4, and math.floor(-4.2) is -5.
  • Non-default parameters cannot follow default parameters. def add(x=5, y): will throw a SyntaxError.

Step-by-Step Worked Board Exam Examples

Example 1: Find the minimum and maximum possible values generated by the code snippet: import random x = 10 + random.randint(1, 4) * 2

random.randint(1, 4) generates integers between 1 and 4 inclusive: {1, 2, 3, 4}.

Minimum value of randint: 1. Minimum x = 10 + (1 * 2) = 10 + 2 = 12.

Maximum value of randint: 4. Maximum x = 10 + (4 * 2) = 10 + 8 = 18.

Possible values for x are {12, 14, 16, 18}.

Ans: Minimum possible value is 12; Maximum possible value is 18.

Example 2: What is the output of the following code? import math print(math.ceil(4.1)) print(math.floor(4.9)) print(math.ceil(-4.9)) print(math.floor(-4.1))

math.ceil(4.1) rounds up to smallest integer >= 4.1: 5.

math.floor(4.9) rounds down to largest integer <= 4.9: 4.

math.ceil(-4.9) rounds up towards positive infinity to smallest integer >= -4.9: -4.

math.floor(-4.1) rounds down towards negative infinity to largest integer <= -4.1: -5.

Ans: 5 4 -4 -5

Common Mistakes to Avoid in Exam

  • Writing random.randint(1, 10) thinking 10 is excluded. randint is INCLUSIVE of both endpoints!
  • Forgetting import math before calling math.sqrt() or math.ceil(), raising a NameError.
  • Defining a function with default arguments first: def calc(a=1, b): causes an immediate SyntaxError.

Target Practice Questions

Q1. What does divmod(17, 5) return?

Correct Answer: (3, 2) (quotient 3, remainder 2).

Q2. What is the return type of math.sqrt(16)?

Correct Answer: <class 'float'> (returns 4.0).

Q3. Which function in the random module returns a floating-point number between 0.0 and 1.0?

Correct Answer: random.random().

Frequently Asked Questions

Q. What is the difference between random.randint(a, b) and random.randrange(a, b)?

randint(a, b) includes both endpoints a and b; randrange(a, b) excludes the upper bound b.

Q. What does math.fabs(x) return?

Returns the absolute value of x as a float (e.g. math.fabs(-5) returns 5.0).

Q. What is the LEGB rule in Python?

The name resolution scope hierarchy: Local, Enclosing, Global, and Built-in.

Q. What is returned by a Python function that has no return statement?

It implicitly returns None.

← View All 9 Chapters in Class 11 Computer Science Formula Book