Showing posts with label Python modules. Show all posts
Showing posts with label Python modules. Show all posts

Monday, March 16, 2026

Lesson 16: Python Modules, Packages, and Pip | Coding Class Series

March 16, 2026 0



Lesson 16: Python Modules, Packages, and Pip | Coding Class Series

Introduction

Welcome to Lesson 16!
In this lesson, we will learn about Python modules and packages, how to import them, and how to install external libraries using pip.
Modules and packages help you organize code and reuse functionality efficiently.


1. What is a Module?

A module is a file containing Python code (functions, classes, variables).
You can reuse modules in multiple programs.

Example: math module

import math

print(math.sqrt(16))  # Output: 4.0
print(math.pi)        # Output: 3.141592653589793

2. Importing Modules

You can import a module in different ways:

# Import the whole module
import math
print(math.factorial(5))  # Output: 120

# Import specific functions
from math import factorial, ceil
print(factorial(6))  # Output: 720
print(ceil(4.2))     # Output: 5

# Import with alias
import math as m
print(m.sqrt(25))    # Output: 5.0

3. Creating Your Own Module

You can create a module by saving functions in a .py file.

my_module.py

def greet(name):
    return f"Hello, {name}!"

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

main.py

import my_module

print(my_module.greet("Alice"))
print(my_module.add(10, 5))

4. What is a Package?

A package is a folder containing multiple modules and an __init__.py file.
It allows better organization of related modules.

Example structure:

my_package/
    __init__.py
    module1.py
    module2.py

Usage:

from my_package import module1
module1.function_name()

5. Installing External Libraries with pip

pip is the Python package manager. You can install external libraries easily.

# Install a library
pip install requests

# Check installed libraries
pip list

Example usage:

import requests

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

6. Upgrading and Uninstalling Packages

# Upgrade a package
pip install --upgrade requests

# Uninstall a package
pip uninstall requests

7. Practice Exercises

  1. Create a module calculator.py with functions for addition, subtraction, multiplication, and division. Import it in another program.
  2. Organize two modules (math_ops.py and string_ops.py) into a package my_utils. Import and use functions.
  3. Install the numpy library using pip and create an array.
  4. Upgrade numpy and then uninstall it using pip commands.


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.


Lesson 7: Functions and Modules | Coding Class Series

March 16, 2026 0



Lesson 7: Functions and Modules | Coding Class Series

Introduction

Welcome to Lesson 7!
In this lesson, we will learn about functions and modules in Python. Functions allow you to reuse code, while modules help you organize your programs efficiently.


1. Functions

A function is a block of code that performs a specific task and can be reused multiple times.

Syntax:

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")  # Output: Hello, Alice!

Key Points:

  • Functions start with the def keyword.
  • Arguments can be passed inside parentheses.
  • return keyword is used to give back a result.

Example with Return:

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

result = add(5, 3)
print(result)  # Output: 8

2. Modules

A module is a file containing Python definitions and functions.
You can import modules to use their functions in your program.

Importing a module:

import math

print(math.sqrt(16))  # Output: 4.0

From module import specific function:

from math import pow

print(pow(2, 3))  # Output: 8.0

Creating your own module:

  1. Create a file my_module.py:
def welcome():
    print("Welcome to my module!")
  1. Import in another file:
import my_module

my_module.welcome()  # Output: Welcome to my module!

3. Advantages of Functions and Modules

  • Reusability: Write once, use multiple times.
  • Organization: Keeps code clean and structured.
  • Efficiency: Saves time and reduces errors.
  • Scalability: Easier to maintain and expand large programs.

Practice Exercises

  1. Create a function multiply(a, b) that returns the product of two numbers.
  2. Write a function that takes a list of numbers and returns the largest number.
  3. Create a module math_utils.py with a function square(n) that returns the square of a number. Import it and test.
  4. Use the random module to generate 5 random numbers between 1 and 50.


Lesson 4: Functions and Modular Programming | Coding Class Series

March 16, 2026 0



Lesson 4: Functions and Modular Programming | Coding Class Series

Introduction

Welcome to Lesson 4!
In this lesson, we will learn about functions – reusable blocks of code – and how to organize your program into modules. Functions make your code cleaner, easier to read, and maintainable.


What is a Function?

A function is a block of code that performs a specific task. You can call it whenever you need to perform that task.

Example in Python:

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")  # Output: Hello, Alice!
greet("Bob")    # Output: Hello, Bob!

Explanation:

  • def greet(name): → defines a function named greet with a parameter name.
  • print(f"Hello, {name}!") → code inside the function.
  • greet("Alice") → calls the function with argument "Alice".

Benefits of Functions

  1. Reusability: Write once, use many times.
  2. Readability: Makes code organized and easier to understand.
  3. Debugging: Easy to test and fix small blocks of code.

Modular Programming

Modular programming is the practice of splitting your code into multiple files or modules. Each module has a specific purpose, making your program scalable and easier to maintain.

Example:

  • math_utils.py → contains functions for math operations
  • main.py → calls functions from math_utils.py
# math_utils.py
def add(a, b):
    return a + b

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

# main.py
from math_utils import add, subtract

print(add(5, 3))      # Output: 8
print(subtract(5, 3)) # Output: 2

Practice Exercises

  1. Write a function to calculate the factorial of a number.
  2. Create a function to check if a string is a palindrome.
  3. Write a module string_utils.py containing functions: reverse_string(), count_vowels().
  4. Import the module in another file and test all the functions.