Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 3 — Math and Random Module

Lesson 40 of 50 in the free Python Programming notes on Siksha Sarovar, written by Rohit Jangra.

Math and Random Module

The math Module

Function/ConstantPurpose
math.sqrt(x)Square root
math.pow(x, y)x raised to power y (returns float)
math.floor(x)Round down to nearest integer
math.ceil(x)Round up to nearest integer
math.factorial(n)n!
math.pi, math.eMathematical constants
math.log(x), math.log10(x)Natural log, base-10 log
math.gcd(a, b)Greatest common divisor
import math

print(math.sqrt(64))          # 8.0
print(math.pow(2, 5))          # 32.0
print(math.floor(4.9))          # 4
print(math.ceil(4.1))            # 5
print(math.factorial(6))          # 720
print(math.pi)                     # 3.141592653589793
print(math.gcd(24, 36))             # 12
print(round(math.sqrt(2), 3))        # 1.414

---

The random Module

FunctionPurpose
random.random()Random float between 0.0 and 1.0
random.randint(a, b)Random integer between a and b (inclusive)
random.randrange(a, b, step)Random integer from a range
random.choice(seq)Random single element from a sequence
random.sample(seq, k)k unique random elements from a sequence
random.shuffle(list)Shuffle a list in place
random.uniform(a, b)Random float between a and b
import random

print(random.random())              # e.g. 0.7294...
print(random.randint(1, 100))         # e.g. 42
print(random.randrange(0, 50, 5))      # e.g. 30 (multiple of 5)

colors = ["red", "green", "blue", "yellow"]
print(random.choice(colors))           # e.g. 'blue'
print(random.sample(colors, 2))         # e.g. ['red', 'yellow']

random.shuffle(colors)
print(colors)                           # colors reordered randomly

print(random.uniform(1, 10))             # e.g. 6.732...

Example: Simulate a dice roll

import random

def roll_dice():
    return random.randint(1, 6)

for _ in range(5):
    print(roll_dice())

Example: OTP generator using random

import random

otp = "".join(str(random.randint(0, 9)) for _ in range(6))
print("Your OTP is:", otp)