Showing posts with label Python third-party libraries. Show all posts
Showing posts with label Python third-party libraries. Show all posts

Monday, March 16, 2026

Lesson 10: Python Modules and Libraries | Coding Class Series

March 16, 2026 0



Lesson 10: Python Modules and Libraries | Coding Class Series

Introduction

Welcome to Lesson 10!
In this lesson, we will learn about Python Modules and Libraries. These are essential for reusing code, extending Python functionality, and making development faster and easier.


1. What is a Module?

A module is a Python file (.py) that contains functions, classes, and variables which you can use in other Python programs.

Example:

# math_module.py
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

You can use this module in another file using import:

import math_module

print(math_module.add(10, 5))       # Output: 15
print(math_module.subtract(10, 5))  # Output: 5

2. Built-in Python Modules

Python has many built-in modules ready to use:

  • math → Mathematical functions
  • random → Random number generation
  • datetime → Date and time functions
  • os → Operating system functionalities

Example:

import math
import random
from datetime import datetime

print(math.sqrt(25))          # Output: 5.0
print(random.randint(1, 10))  # Output: Random number between 1 and 10
print(datetime.now())         # Output: Current date and time

3. Installing Third-Party Libraries

Python libraries like requests, numpy, pandas, etc., can be installed using pip:

pip install requests
pip install numpy

Example using requests:

import requests

response = requests.get("https://api.github.com")
print(response.status_code)  # Output: 200

4. Creating Your Own Library

You can combine multiple modules into a package (your own library):

my_library/
│
├── __init__.py
├── math_utils.py
└── string_utils.py

Usage:

from my_library.math_utils import add
print(add(5, 10))  # Output: 15

5. Practice Exercises

  1. Create a module calc.py with functions multiply and divide. Import it in another file and test it.
  2. Use the random module to generate a random list of numbers and find the max and min.
  3. Install requests and fetch data from any public API.
  4. Create a simple package with 2 modules and use it in a program.