3: Importing and Creating Modules in Py
Python Standard Libraries: math, random, time
Presented by Mr. Anto Jegan. V
Department of Computer Science
Introduction to Modules - Part 1
module is a Python file that contains functions, variables, or classes.
odules help to organize code and avoid repetition.
nalogy: A module is like a book in a library; it contains specific knowledge that can be reus
Introduction to Modules - Part 2
• Python has built-in modules and allows creation of user-defined modules.
• Example built-in modules: math, random, time, os, sys.
• Example user-defined module: mymodule.py containing custom functions.
Types of Modules - Part 1
1. Built-in Modules: Provided by Python (e.g., math, random, time).
2. User-defined Modules: Created by programmers for reuse.
Types of Modules - Part 2
3. External Modules: Downloaded from Python Package Index (PyPI).
Examples: numpy, pandas, requests, matplotlib.
Importing Modules - Part 1
• import module_name: imports entire module.
• from module_name import function_name: imports specific function.
• import module_name as alias: imports module with alias for convenience.
Importing Modules - Part 2
Examples:
import math
print(math.sqrt(25))
from math import pi
print(pi)
import random as rnd
print(rnd.randint(1, 10))
Creating Custom Modules - Part 1
• Steps to create a module:
1. Create a Python file (e.g., mymodule.py).
2. Define functions, classes, or variables inside it.
Creating Custom Modules - Part 2
• Import and use in another file:
import mymodule
mymodule.function_name()
• Example: mymodule.py contains add(a,b) and sub(a,b) functions.
__name__ == '__main__' - Part 1
• When a module is run directly, __name__ is '__main__'.
• When imported, __name__ is the module's name.
__name__ == '__main__' - Part 2
Example:
if __name__ == '__main__':
print('This module is run directly')
• Useful for testing and separating executable code from definitions.
math Module - Part 1
• Provides access to mathematical functions.
• Common functions: sqrt(), pow(), ceil(), floor(), factorial(), sin(), cos()
• Example: math.sqrt(16) → 4.0
math Module - Part 2
• Real-world example: Calculating distances, areas, and scientific computations.
• Example:
import math
r=5
area = math.pi * math.pow(r,2)
print(area)
random Module - Part 1
• Generates pseudo-random numbers.
• Functions: random(), randint(), choice(), shuffle()
• Useful in games, simulations, and testing.
random Module - Part 2
Example:
import random
numbers = [1,2,3,4,5]
random.shuffle(numbers)
print(random.choice(numbers))
time Module - Part 1
• Provides time-related functions.
• Functions: time(), sleep(), ctime(), localtime(), strftime()
• Useful for delays, timestamps, and performance measurement.
time Module - Part 2
Example:
import time
print(time.ctime())
print('Waiting for 2 seconds...')
time.sleep(2)
print('Done')
Real-Life Applications - Part 1
• math: Used in engineering, physics, analytics, and simulations.
• random: Used in games, randomized testing, and AI shuffling.
Real-Life Applications - Part 2
• time: Used in animations, schedulers, and system monitoring.
• Combining modules: e.g., math + random + time to simulate real-world scenarios.
Summary & Practice - Part 1
• Modules make code reusable, organized, and maintainable.
• Built-in modules simplify complex operations.
• Custom modules improve project structure.
Summary & Practice - Part 2
Practice Questions:
1. Create a module for area and perimeter calculations.
2. Generate 10 random integers and display them.
3. Display current time and measure execution delay.
4. Discuss benefits of modular programming in teamwork.