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 |
|
Critical board exam distinction: randint includes the upper limit, while randrange excludes the upper limit. |
| 2 | Math Ceil and Floor Integer Bounds |
|
ceil always rounds up towards positive infinity; floor always rounds down towards negative infinity. |
| 3 | The divmod() Tuple Return Formula |
|
Calculates integer quotient and modulus simultaneously in a single efficient CPU operation. |
| 4 | Default Parameter Rule Syntax |
|
In function definitions, all parameters with default values must appear after all parameters without default values. |
| 5 | Statistics Module Measures of Central Tendency |
|
Standard descriptive statistical functions provided by Python's built-in statistics library. |
| 6 | Math pow() vs Built-in pow() vs ** |
|
Built-in pow() supports 3-argument modular exponentiation, whereas math.pow() always converts outputs to floats. |
| 7 | Random Choice Uniform Distribution |
|
Selects a random element uniformly from any non-empty sequence (list, tuple, or string). |
| 8 | Greatest Common Divisor (math.gcd) |
|
Standard mathematical function to compute HCF / GCD of two integers. |
| 9 | Function Variable Scope (LEGB Rule) |
|
Python's namespace lookup order to resolve variable identifiers inside functions. |
| 10 | Round Function Half-to-Even Rule |
|
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}.
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.
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.