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
, andtime
. - 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:
Module | Purpose |
---|---|
math | Math functions (sqrt ,pi , etc.) |
random | Generate random numbers or choices |
time | Work with time and delays |
datetime | Work with dates and times |
os | Interact 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!")

๐ฆ 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:
- Welcomes the user.
- Waits 1 second.
- 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
, andtime
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