Monday, 20 April 2026

April Python Bootcamp Day 13

 

What is a Module?

A module is a single Python file (.py) that contains functions, variables, or classes which can be reused in other programs.

Example:
If you create a file named utils.py, it becomes a module.

Why modules are important:

  • Promote code reusability
  • Help in organizing large codebases
  • Reduce redundancy

What is a Package?

A package is a folder that contains multiple modules.

Structure example:

my_package/
├── module1.py
├── module2.py
└── __init__.py

Packages allow you to structure your project logically and scale your code efficiently.


Importing Modules

There are multiple ways to import modules:

1. Import Entire Module

import math
print(math.sqrt(16))

2. Import Specific Functions

from math import sqrt, ceil

print(sqrt(16))
print(ceil(4.2))

3. Using Module Alias

import math as m
print(m.factorial(5))

Built-in Modules in Python

Python provides many built-in modules that simplify development.


Math Module

Used for mathematical operations.

import math

print(math.sqrt(16))
print(math.ceil(4.2))
print(math.floor(4.9))
print(math.pow(2, 3))
print(math.factorial(5))

Common functions:

  • sqrt()
  • ceil()
  • floor()
  • pow()
  • factorial()

Random Module

Used for generating random values.

import random

print(random.random())
print(random.randint(1, 10))
print(random.choice([1, 2, 3, 4]))

Shuffling example:

lst = [1, 2, 3, 4]
random.shuffle(lst)
print(lst)

Other function:

  • uniform(a, b) → random float between a and b

Datetime Module

Used for working with dates and time.

from datetime import datetime, date, timedelta

print(datetime.now())
print(date.today())

d = datetime.now()
print(d.strftime("%Y-%m-%d"))

new_date = d + timedelta(days=5)
print(new_date)

Key functionalities:

  • Current date and time
  • Formatting dates
  • Date arithmetic

OS Module

Used for interacting with the operating system.

import os

print(os.getcwd())
print(os.listdir())

Common operations:

  • Get current directory
  • List files
  • Create/delete folders
  • Rename files

Key Takeaways

  • Modules help reuse code
  • Packages help organize large projects
  • Built-in modules save development time
  • Proper imports improve code readability

Assignment Questions

Basic Level

  1. Import the math module and find square root of a number
  2. Import specific functions from math and use ceil() and floor()
  3. Generate a random number between 1 and 50
  4. Print current date using datetime
  5. Print current working directory using os

Intermediate Level

  1. Generate a list of 5 random numbers using random.randint()
  2. Shuffle a list of numbers using random.shuffle()
  3. Format current date as DD-MM-YYYY
  4. Create a program to add 7 days to current date
  5. List all files in your current directory

Advanced Level

  1. Create your own module with functions (e.g., add, subtract) and import it
  2. Create a package with at least 2 modules and use them
  3. Build a mini project using math and random (e.g., number guessing game)
  4. Write a script to organize files in a folder using os
  5. Combine datetime and os to log file creation time

Summary

Modules and packages are fundamental for writing scalable Python applications.

  • Modules allow code reuse
  • Packages provide structure
  • Built-in modules handle complex operations easily

Understanding this concept is essential before moving into:

  • Large projects
  • Frameworks
  • Real-world software development

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (248) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (29) Azure (10) BI (10) Books (262) Bootcamp (9) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (31) Data Analytics (22) data management (15) Data Science (347) Data Strucures (17) Deep Learning (154) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (70) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (42) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (286) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (14) PHP (20) Projects (32) pytho (1) Python (1312) Python Coding Challenge (1128) Python Mistakes (51) Python Quiz (481) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (49) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)