📚 Modules

Python Modules

पायथन मॉड्यूल

⏱ 2 hr4 topicsInteractive
🎯 By the end: You can import modules in different ways and use the common functions of math, random and statistics.

You don't have to write everything from scratch. A module is a ready-made file of useful functions someone has already written — you just import it and use it. Python ships with a big standard library of them. This chapter covers how to import, and the three modules the syllabus expects: math, random and statistics.

1How to import

There are a few ways to bring a module's tools into your program:

FormThen you use…
import mathmath.sqrt(16) (module name prefix)
from math import sqrtsqrt(16) (just the name)
import math as mm.sqrt(16) (a short alias)
import math is the safest and clearest — the math. prefix tells anyone reading your code exactly where sqrt comes from. Run the playground to see it work:
Importing a module▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • import math then use math.sqrt(...); from math import sqrt lets you use sqrt(...) directly.
  • import math as m creates a short alias.
  • The module-prefix form (math.sqrt) is clearest about where a function comes from.

2The math module

The math module provides mathematical constants and functions:

Function / constantGives
math.pi, math.eThe constants π (3.14159…) and e
math.sqrt(x)Square root
math.pow(x, y)x to the power y (as a float)
math.ceil(x)Round UP to the next whole number
math.floor(x)Round DOWN to the whole number below
math.fabs(x)Absolute value (always positive)

It also has the trigonometry functions math.sin(), math.cos() and math.tan(). Try the playground:

math module▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • Constants: math.pi, math.e.
  • sqrt, pow, fabs, and trig (sin/cos/tan).
  • ceil rounds UP, floor rounds DOWN.

3random and statistics

The random module produces random numbers — perfect for games, simulations and shuffling:

  • random.random() — a float from 0.0 up to (not including) 1.0.
  • random.randint(a, b) — a whole number from a to b inclusive (e.g. a dice roll).
  • random.randrange(start, stop, step) — like range(), but picks one value.

Run it a few times — the output changes each run:

import statistics
data = [10, 20, 20, 40]
statistics.mean(data)     # 22.5  (average)
statistics.median(data)   # 20    (middle value)
statistics.mode(data)     # 20    (most frequent)

The statistics module (shown above) gives mean() (average), median() (the middle value when sorted) and mode() (the most common value).

random module (run me a few times!)▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • random.random() gives 0.0–1.0; randint(a,b) is inclusive of both ends; randrange works like range().
  • statistics.mean = average, median = middle value, mode = most frequent.
  • Random output differs on every run.

4Project: Scientific Calculator

Bring the math module together into a mini Scientific Calculator. Change num and run it — then add your own operations:

Scientific Calculator▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • Modules let you build powerful tools quickly without reinventing the maths.
  • math.sqrt, math.pow, math.ceil and math.floor cover common calculator needs.
  • Combine modules with your earlier skills (variables, operators, conditions).

★ Project: extend the Scientific Calculator

Using the playground above:

  1. Add a line that prints math.pi and a circle's area for radius = num (area = pi × r × r).
  2. Use math.fabs() on a negative number and print the result.
  3. Import random and print a random integer 'lucky number' between 1 and 100.
  4. Bonus: try 'from math import sqrt' and call sqrt() without the math. prefix.

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.