CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Python · 7 min · free

Python Modules: math, random and statistics

7 min5 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Learn Python modules for CBSE: import vs from import, math functions, random, randint, randrange, mean, median, mode, own module. A module is a Python file of ready code, and import brings it into your program.

Follows the syllabus of: CBSE Class 11 Computer Science (083), CBSE Class 11 Computer Science Essentials (083), CBSE Class 12 Computer Science (083)

On screen in this lesson

What is a module?

A module is a Python file of ready code
It holds functions and values, like pi
import brings a module into your program
math, random and statistics come with Python

import vs from import

StyleHow you callNote
import mathmath.sqrt(81)clear, safe
from math importsqrt(81)shorter
import math as mtmt.sqrt(81)short nickname

Which values can come?

CallPossible valuesLast value?
random()0.0 up to 1.01.0 never
randint(1, 6)1 to 66 can come
randrange(1, 6)1 to 56 never
randrange(0, 10, 2)0, 2, 4, 6, 810 never

Importing your own module

In main.pyWhat it doesResult
import shoploads shop.pyready to use
shop.gst(200)18% of 20036.0
shop.shop_namereads variableSharma Stores

Quick recap

import math, then math.sqrt(); or from math import sqrt
math: pi, e, sqrt, ceil, floor, pow, fabs, sin, cos, tan
random: randint(1, 6) can give 6; randrange(1, 6) cannot
statistics: mean, median, mode
Any .py file you write can be imported

Quick answers

Can randrange(1, 6) give 6?

No. randrange never includes the stop value, but randint(1, 6) can give 6.

What is math.ceil(-4.2)?

-4, because ceil rounds up to the bigger number.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Do you need to write your own code for square roots, dice rolls or averages? No. Python already has it ready. Today we will learn the math, random and statistics modules, and even make a module of our own.

First, a new term. A module is a Python file full of ready code. It holds functions, which are named blocks of code, and values such as pi. The import statement brings a module into your program so you can use it. The math, random and statistics modules come free with Python, so nothing extra is installed.

The first way is import, followed by the module name. After that, we write the module name, a dot, and then the item we want. Math dot pi is the circle constant, about three point one four. Math dot E is another famous constant, about two point seven one eight. The dot tells Python which module the name belongs to.

The second way is from, module name, import, and then the names you want. Now we can write square root without math dot in front. Square root of eighty one is nine point zero. Round is a built-in function, and round of pi with two keeps two decimal places, so three point one four.

Let us compare the styles. With import math, every call starts with math dot, which is clear and avoids name clashes. With from math import square root, the call is shorter, but that name could clash with your own variable. With import math as M T, we give math a short nickname, called an alias.

Square root of one hundred forty four is twelve point zero, and it always gives a decimal answer. Ceil, short for ceiling, rounds up to the next whole number, so four point two becomes five. Floor rounds down, so four point eight becomes four. Pause and predict. What is ceil of minus four point two? Up means towards the bigger number, and minus four is bigger than minus five. So the answer is minus four.

Pow means power, so pow of two and ten is two multiplied by itself ten times. The math version always gives a decimal, one thousand twenty four point zero. The double star operator also means power, but it keeps whole numbers whole. Fabs gives the absolute value, which is the distance from zero, as a decimal. The built-in abs keeps the whole number, seven.

Sin, cos and tan are the trigonometry functions from your maths class. There is one catch. Python measures angles in radians, not degrees. So math dot radians changes ninety degrees into radians first, and sin of ninety degrees is one point zero. Cos of zero is one point zero. Pi by four radians is forty five degrees, and tan of that is one. We round it, because computers store such decimals slightly off.

The random module picks numbers by chance, like a dice roll or a lucky draw. Normally the numbers change on every run. Seed fixes the starting point, so the same numbers come every time, which helps us check our work. Random of nothing gives a decimal from zero up to one. Randint of one and six acts like a dice. Randrange of zero, ten and two picks from zero, two, four, six and eight.

Exams love asking which values are possible. Random gives any decimal from zero point zero, but never exactly one. Randint includes both ends, so six can come. Randrange works like range, so the stop value is never included, and six cannot come. With a step of two, randrange of zero, ten and two gives only even numbers up to eight.

The statistics module finds averages. Here are a batter's runs in five matches, kept in a list, which is a group of values in square brackets. Mean adds them and divides by the count, so two hundred divided by five is forty. Median sorts the values and picks the middle one, which is forty five. Mode gives the value that appears most often, and twelve appears twice.

What if the count is even, and there is no single middle value? Sorted, the marks are sixty, seventy, eighty and ninety. Median takes the two middle values, seventy and eighty. It gives their average, seventy five point zero.

Any Python file you write is also a module. Save this code in a file named shop.py. Its function, G S T, finds eighteen percent goods and services tax on a price. Eighteen percent is a common rate in India. It also has a variable with the shop's name.

Now open a second file, main.py, in the same folder as shop.py. The line import shop loads your file, just as import math loads the math module. Then shop dot G S T of two hundred finds eighteen percent of two hundred, which is thirty six point zero. And shop dot shop underscore name reads the variable, Sharma Stores. Do not write dot py in the import line, only the file name.

Let us revise what we learned today. Use import with a dot, or from import for shorter names. The math module gives constants like pi and e, and functions like square root, ceil and floor. In random, randint includes the last value, but randrange does not. Statistics gives mean, median and mode. And any Python file you write can be imported as your own module.

Courses that teach this

CourseUnit
CBSE Class 11 Computer Science (083)Computational Thinking and Programming - I
CBSE Class 11 Computer Science Essentials (083)Computational Thinking and Programming - I
CBSE Class 12 Computer Science (083)Computational Thinking and Programming – 2
Programming All levels PythonModules and Standard Library

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →