Python Modules Practice Questions with Solutions

Python modules help you organize code into reusable files. Python also provides many built-in modules, such as math, random, and datetime, that make programming easier. In this practice set, you’ll learn how to import modules, use built-in functions, create your own modules, and solve beginner-friendly Python programs. Python Modules practice questions with solutions help to understand the concepts.


1. Python Program to Import the Math Module

Difficulty: Foundation

Problem Statement

Write a Python program to import the math module and find the square root of 81.

Python Solution

import math

result = math.sqrt(81)

print(result)

Sample Output

9.0

Explanation

The math module provides mathematical functions like sqrt().

Concepts Covered

  • import
  • math module
  • sqrt()

2. Python Program to Find the Value of Pi

Difficulty: Foundation

Problem Statement

Write a Python program to print the value of π using the math module.

Python Solution

import math

print(math.pi)

Sample Output

3.141592653589793

Explanation

The math.pi constant returns the value of π.

Concepts Covered

  • math.pi

3. Python Program to Generate a Random Number

Difficulty: Foundation

Problem Statement

Write a Python program to generate a random number between 1 and 100.

Python Solution

import random

print(random.randint(1, 100))

Sample Output

57

Note: Your output may be different because the number is generated randomly.

Explanation

The randint() function returns a random integer within the specified range.

Concepts Covered

  • random module
  • randint()

4. Python Program to Choose a Random Item from a List

Difficulty: Foundation

Problem Statement

Write a Python program to randomly select a fruit from a list.

Python Solution

import random

fruits = ["Apple", "Banana", "Mango", "Orange"]

print(random.choice(fruits))

Sample Output

Mango

Note: The output may vary each time you run the program.

Explanation

The choice() function returns a random element from a sequence.

Concepts Covered

  • random.choice()

5. Python Program to Display the Current Date

Difficulty: Practice

Problem Statement

Write a Python program to print the current date using the datetime module.

Python Solution

from datetime import date

today = date.today()

print(today)

Sample Output

2026-07-31

Explanation

The today() method returns the current system date.

Concepts Covered

  • datetime module
  • date.today()

6. Python Program to Import a Specific Function

Difficulty: Practice

Problem Statement

Write a Python program to import only the sqrt() function from the math module.

Python Solution

from math import sqrt

print(sqrt(144))

Sample Output

12.0

Explanation

You can import only the required function instead of the entire module.

Concepts Covered

  • from…import

7. Python Program to Import a Module with an Alias

Difficulty: Practice

Problem Statement

Write a Python program to import the math module using the alias m.

Python Solution

import math as m

print(m.factorial(5))

Sample Output

120

Explanation

The as keyword creates a shorter alias for a module.

Concepts Covered

  • import as
  • Module Alias

8. Python Program to Create a Custom Module

Difficulty: Challenge

Problem Statement

Create a custom module named calculator.py with an add() function and use it in another Python file.

calculator.py

def add(a, b):
    return a + b

main.py

import calculator

print(calculator.add(15, 10))

Sample Output

25

Explanation

Custom modules help organize reusable code into separate files.

Concepts Covered

  • Custom Module
  • import

9. Python Program to Generate a Random Floating-Point Number

Difficulty: Challenge

Problem Statement

Write a Python program to generate a random floating-point number between 0 and 1.

Python Solution

import random

print(random.random())

Sample Output

0.684527913

Note: The output will be different every time you run the program.

Explanation

The random() function returns a random float between 0.0 and 1.0.

Concepts Covered

  • random.random()

10. Python Program to Calculate the Factorial of a Number Using the Math Module

Difficulty: Challenge

Problem Statement

Write a Python program to calculate the factorial of 6 using the math module.

Python Solution

import math

print(math.factorial(6))

Sample Output

720

Explanation

The factorial() function returns the factorial of a non-negative integer.

Concepts Covered

  • math.factorial()

Chapter Summary

After completing this chapter, you have learned:

  • Importing modules
  • Using the math module
  • Using the random module
  • Using the datetime module
  • Importing specific functions
  • Using module aliases
  • Creating custom modules
  • Generating random numbers
  • Working with mathematical functions

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top