👋 Work With Me

I help startups and teams build production-ready apps with Django, Flask, and FastAPI.

Let’s Talk →

I'm always excited to take on new projects and collaborate with innovative minds.

Address

No 7 Street E, Federal Low-cost Housing Estate, Kuje, Abuja 903101, Federal Capital Territory

Social Links

Tutorials

Python Tutorial Series – Lesson 12: Modules and Packages in Python

Python Tutorial for Beginners (Lesson 12): Learn about modules and packages in Python, how to import built-in modules, create custom modules, install external packages with pip, and organize code effectively.

Python Tutorial Series – Lesson 12: Modules and Packages in Python

Welcome back to the Python Tutorial Series for Beginners! 🚀

In Lesson 11, we explored functions and learned how to organize reusable blocks of code.

Now in Lesson 12, we’ll step into the world of Modules and Packages – the tools that let us organize larger projects, avoid repetition, and use thousands of built-in and third-party libraries.

By the end of this lesson, you’ll understand:

  • What modules are and how to use them
  • Creating your own Python module
  • Importing and using functions from modules
  • The difference between built-in and custom modules
  • What packages are and why they matter
  • The __init__.py file
  • Installing and using external packages with pip
  • Beginner exercises

🔹 1. What is a Module?

👉 A module is simply a Python file (.py) that contains code (functions, variables, or classes).
You can import and reuse it in other Python programs.

💡 Think of a module as a toolbox: instead of rewriting code, you just grab the tool you need.


🔹 2. Importing Built-in Modules

Python comes with many built-in modules.

 
import math

print(math.sqrt(16))   # square root
print(math.pi)         # value of pi
print(math.factorial(5))  # factorial

👉 Output:

 
4.0
3.141592653589793
120

Other examples:

  • random → generate random numbers
  • datetime → work with dates and times
  • os → interact with the operating system

🔹 3. Creating Your Own Module

Let’s create a file called mymodule.py:

 
# mymodule.py
def greet(name):
   return f"Hello, {name}!"
   
def add(a, b):
   return a + b

Now, in another file (main.py):

 
import mymodule

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

👉 Output:

 
Hello, Alice!
15

🔹 4. Importing Specific Functions

Instead of importing everything, you can import only what you need:

 
from math import sqrt, pi

print(sqrt(25))
print(pi)
 

👉 Output:

 
5.0
3.141592653589793

🔹 5. Renaming Modules (Alias)

You can give a module an alias for convenience:

 
import math as m

print(m.sqrt(49))

👉 Output: 7.0


🔹 6. What is a Package?

👉 A package is a collection of modules, stored in a directory with a special __init__.py file.

Example structure:

 
mypackage/
   __init__.py
   module1.py
   module2.py
  • __init__.py makes Python treat the folder as a package.
  • You can then import modules like:
 
from mypackage import module1 

🔹 7. Installing External Packages

Python has a huge ecosystem of external packages.
You install them using pip (Python’s package manager).

 
pip install requests 

Then use it in your code:

 
import requests

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

👉 Output:

 
200 

🔹 8. Real-World Use Cases

Math Calculations (math module):

 
import math
print(math.pow(2, 5))  # 32.0

Random Password Generator (random module):

 
import random
import string

password = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
print("Generated password:", password)

Web API Request (requests package):

 
import requests
data = requests.get("https://jsonplaceholder.typicode.com/todos/1").json()
print(data)

🔹 9. Exercises for Beginners

Try these in a new file (lesson12.py):

  1. Import the datetime module and print today’s date.
  2. Create your own module with a function that returns the square of a number. Import and use it.
  3. Use the random module to simulate rolling a dice (1–6).
  4. Install and use the requests package to fetch data from an API.
  5. Create a small package with two modules: one for math operations and one for greetings.

🎯 Recap

In this lesson, you learned:
✅ Modules help organize and reuse code
✅ Python provides many built-in modules (like math, random, os)
✅ You can create and import your own modules
✅ Packages are directories containing multiple modules
pip helps you install and use external libraries

Next up: Lesson 13 – File Handling in Python 📂 (reading, writing, and managing files).

💼 Need a Developer?

I'm Kingsley Odume, a Django, Flask, and FastAPI developer with experience building SaaS platforms, APIs, and modern web apps. If you're a recruiter or business owner looking for a reliable software developer, let's connect!

🚀 Hire Me

Python, Python tutorials, Python Beginners series, Python Step-byStep
4 min read
Aug 20, 2025
By Kingsley Odume
Share

Leave a comment

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

Related posts

Aug 29, 2025 • 3 min read
Step-by-Step: Building a SaaS App with Django + Stripe Payments

Learn how to build a real-world SaaS app with Django and Stripe paymen...

Aug 21, 2025 • 3 min read
Final Wrap-Up: Python Tutorial Series for Beginners

Congratulations on completing the Python Tutorial Series! 🚀 In this w...

Aug 20, 2025 • 4 min read
Final Lesson: Mini Project – Putting It All Together in Python

In this final lesson, we’ll bring all those concepts together in a Min...

Your experience on this site will be improved by allowing cookies. Cookie Policy