Skip to content

Python! Modules And Packages | Lectures 14

Lecture 14 Modules Packages

Let’s Start Python Programming For Beginners to Advanced. Modules & Packages, Lectures 14

๐Ÿ Lecture 14: Modules & Packages

๐ŸŽฏ Goal

By the end of this lecture, you will:

  • Understand what modules and packages are in Python.
  • Know how to import built-in modules like random, math, and time.
  • Be able to install and use third-party packages using pip.
  • Build a simple program that uses imported code.

๐Ÿง  What Are Modules and Packages?

Module

A module is just a .py file with Python code. It can contain functions, variables, or classes that you can reuse in your programs.

Think of it like a toolbox โ€” each module gives you tools (functions) to do something useful.

Example:

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

A package is a collection of modules. Itโ€™s like a toolbox with smaller toolboxes inside.

Python has many built-in modules, and thousands of third-party packages made by other developers.

๐Ÿ”’ Built-in Modules โ€“ Ready to Use!

You donโ€™t need to install these โ€” they come with Python.

Common Built-in Modules:
ModulePurpose
mathMath functions (sqrt,pi, etc.)
randomGenerate random numbers or choices
timeWork with time and delays
datetimeWork with dates and times
osInteract with the operating system
๐ŸŽฒ Example: Using random Module

Use random to generate random numbers or pick from a list.

import random

# Pick a random number between 1 and 10
print(random.randint(1, 10))

# Choose a random color
colors = ["red", "blue", "green"]
print(random.choice(colors))
๐Ÿ“ Example: Using math Module

The math The module gives you powerful math tools.

import math

print(math.pi)        # 3.14159...
print(math.sqrt(25))  # Square root of 25 โ†’ 5.0
print(math.ceil(4.2)) # Rounds up โ†’ 5
print(math.floor(4.8))# Rounds down โ†’ 4
โฑ๏ธ Example: Using time Module

Use time.sleep() to pause your program for a bit โ€” great for games or animations.

import time

print("Starting game...")
time.sleep(2)  # Wait 2 seconds
print("Ready!")
Lecture 14 Modules Packages
Lecture 14 Modules and Packages

๐Ÿ“ฆ Installing and Using Third-Party Packages

Python also lets you use code written by others through packages.

To install a package, we use pip โ€” Pythonโ€™s package installer.

Step 1: Open Terminal or Command Prompt

Type:

pip install package-name
Step 2: Use the Installed Package

Example: Install and use requests (for getting data from the web)

pip install requests

Then in Python:

import requests

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

๐Ÿงช Try It Yourself!

Write a program that:

  1. Welcomes the user.
  2. Waits 1 second.
  3. Shows a random joke from a list.

๐Ÿ’ก Sample Code:

import time
import random

print("Welcome to the Joke Machine!")
time.sleep(1)

jokes = [
    "Why did the scarecrow win an award? Because he was outstanding in his field!",
    "What do you call fake spaghetti? An impasta!",
    "Why don't skeletons fight each other? They donโ€™t have the guts!"
]

print(random.choice(jokes))

๐Ÿš€ Challenge (Optional)

Make a Magic 8-Ball Game using random.

import random

answers = [
    "Yes!", "No.", "Maybe", 
    "Ask again later...", "Definitely!", "I don't think so."
]

input("Ask a yes/no question: ")
print("๐Ÿ”ฎ", random.choice(answers))

๐Ÿ‘‰ Example Run:

Ask a yes/no question: Will I have pizza tonight?
๐Ÿ”ฎ Yes!

๐Ÿง’ Kids Corner

๐Ÿง  Imagine you have a robot friend who loves collecting tools.
Each time you say:

โ€œRobot, open the random toolbox!โ€

It pulls out a random toy from its bag!

๐Ÿค– Robot says:

Here's a surprise! You never know what you'll get next!

๐Ÿ“Œ Summary

  • A module is a file containing Python code.
  • Use import to bring in a module.
  • Use built-in modules like random, math, and time to add powerful features.
  • Use pip install to add third-party packages to your Python toolbox.
Stay Updated

If you found this information useful, donโ€™t forget to bookmark this page and Share and leave your feedback in the comment section below.

Python Programming For Beginners To Advanced

Python Programming For Beginners! Lecture 1

Python Programming! Variables and Data Types | Lectures 2

Python Programming! Input And Output | Lectures 3

Python Programming! Operators in Python Lectures 4

Python Programming! Conditional Statements Lectures 5

Python! Loops For and While | Lectures 6

Python! Building A Mini Game | Lectures 7

Python! Functions | Lectures 8

Python! Storing Multiple Values | Lectures 9

Python! Tuples Sets And Dictionaries | Lectures 10

Python! Working with Strings | Lectures 11

Python Programming! File Handling | Lectures 12

Python Programming! Error Handling | Lectures 13

Python! Modules And Packages | Lectures 14

Python Programming! File Handling | Lectures 15

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.

Leave a Reply

Your email address will not be published. Required fields are marked *