Python Modules (Part 2) | Random Module & Statistics Module | CBSE Class 11 Computer Science
Class 11 · Computer Science
Python Modules (Part 2)
Python provides several built-in modules that simplify programming by offering ready-to-use functions. In this article, we will study the random module for generating random numbers and the statistics module for performing basic statistical calculations.
Learning Objectives
- Generate random numbers using the random module.
- Understand the difference between random(), randint(), and randrange().
- Calculate mean, median, and mode using the statistics module.
- Apply these modules in real-life programs.
Random Module
The random module is used to generate random numbers and random selections. It is widely used in games, simulations, password generation, quizzes, and testing applications.
Importing the Module
import random
1. random.random()
The random() function returns a random floating-point number greater than or equal to 0.0 and less than 1.0.
Syntax
random.random()
Example
import random
print(random.random())
Possible Output
0.582143
Note: The output changes every time the program is executed.
2. random.randint()
The randint() function returns a random integer between the specified starting and ending values. Both limits are included.
Syntax
random.randint(start,end)
Example
import random
print(random.randint(1,10))
Possible Output
7
3. random.randrange()
The randrange() function returns a randomly selected integer from a specified range.
Syntax
random.randrange(start,stop,step)
Example 1
import random
print(random.randrange(1,10))
Possible Output
4
Example 2
import random
print(random.randrange(10,51,10))
Possible Output
30
Difference between randint() and randrange()
| randint() | randrange() |
|---|---|
| Both start and end values are included. | Stop value is excluded. |
| Cannot specify a step value. | Can specify a step value. |
| Used for simple random integers. | Used for random values from a range. |
Statistics Module
The statistics module provides functions to calculate statistical measures such as mean, median, and mode.
Importing the Module
import statistics
1. statistics.mean()
The mean() function returns the arithmetic average of numeric values.
Syntax
statistics.mean(data)
Example
import statistics
marks=[80,90,85,95,100]
print(statistics.mean(marks))
Output
90
2. statistics.median()
The median() function returns the middle value of the sorted data.
Syntax
statistics.median(data)
Example
import statistics
marks=[80,90,85,95,100]
print(statistics.median(marks))
Output
90
3. statistics.mode()
The mode() function returns the value that occurs most frequently in the data.
Syntax
statistics.mode(data)
Example
import statistics
numbers=[2,3,5,2,7,2,8]
print(statistics.mode(numbers))
Output
2
Difference between Mean, Median and Mode
| Mean | Median | Mode |
|---|---|---|
| Arithmetic average. | Middle value. | Most frequent value. |
| Uses all observations. | Depends on the middle position. | Depends on frequency. |
| Can be affected by extreme values. | Less affected by extreme values. | Not affected by extreme values. |
Practical Programs
Program 1: Generate a Random Number
import random
print(random.randint(1,100))
Program 2: Roll a Dice
import random
print("Dice =",random.randint(1,6))
Program 3: Generate a Random Even Number
import random
print(random.randrange(2,21,2))
Program 4: Calculate Mean
import statistics
marks=[75,82,91,88,94]
print(statistics.mean(marks))
Program 5: Calculate Median
import statistics
marks=[75,82,91,88,94]
print(statistics.median(marks))
Program 6: Calculate Mode
import statistics
numbers=[10,20,20,30,40]
print(statistics.mode(numbers))
Real-Life Applications
- Lottery and lucky draw systems.
- Online quiz applications.
- Password and OTP generation.
- Game development.
- Student result analysis.
- Survey data analysis.
- Statistical reporting.
Summary Table
| Function | Purpose |
|---|---|
random() |
Returns a random float between 0.0 and 1.0. |
randint() |
Returns a random integer including both limits. |
randrange() |
Returns a random value from a range. |
mean() |
Calculates the arithmetic mean. |
median() |
Returns the middle value. |
mode() |
Returns the most frequent value. |
Common Programming Mistakes
- Assuming
random()returns an integer. - Confusing
randint()withrandrange(). - Forgetting that the stop value in
randrange()is excluded. - Using the statistics module with non-numeric data.
- Expecting the same random number every time a program runs.
CBSE Important Programs
- Generate a random number.
- Simulate rolling a dice.
- Generate a random even number.
- Generate a random odd number.
- Calculate the mean of marks.
- Calculate the median of marks.
- Find the mode of a list.
- Compare mean, median, and mode.
- Create a simple lottery program.
- Create a number guessing game.
Viva Questions
- What is the purpose of the random module?
- What is the difference between
random()andrandint()? - How is
randrange()different fromrandint()? - What is the purpose of the statistics module?
- What is the arithmetic mean?
- How is the median calculated?
- What is mode?
- Which function returns a floating-point random number?
- Which function returns the most frequent value?
- Give two applications of the random module.
Exam Tips
- Remember that
random()returns a floating-point value. randint()includes both start and end values.randrange()excludes the stop value.- Use the statistics module only with numeric data.
- Practice the difference between mean, median, and mode.
Quick Revision
random()generates a random float.randint()generates a random integer including both limits.randrange()generates a random value from a range.mean()calculates the arithmetic average.median()returns the middle value.mode()returns the most frequent value.