Computer Science

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() with randrange().
  • 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

  1. Generate a random number.
  2. Simulate rolling a dice.
  3. Generate a random even number.
  4. Generate a random odd number.
  5. Calculate the mean of marks.
  6. Calculate the median of marks.
  7. Find the mode of a list.
  8. Compare mean, median, and mode.
  9. Create a simple lottery program.
  10. Create a number guessing game.

Viva Questions

  1. What is the purpose of the random module?
  2. What is the difference between random() and randint()?
  3. How is randrange() different from randint()?
  4. What is the purpose of the statistics module?
  5. What is the arithmetic mean?
  6. How is the median calculated?
  7. What is mode?
  8. Which function returns a floating-point random number?
  9. Which function returns the most frequent value?
  10. 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.