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.
No 7 Street E, Federal Low-cost Housing Estate, Kuje, Abuja 903101, Federal Capital Territory
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.
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:
__init__.py filepip👉 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.
Python comes with many built-in modules.
👉 Output:
Other examples:
random → generate random numbersdatetime → work with dates and timesos → interact with the operating systemLet’s create a file called mymodule.py:
Now, in another file (main.py):
👉 Output:
Instead of importing everything, you can import only what you need:
👉 Output:
You can give a module an alias for convenience:
👉 Output: 7.0
👉 A package is a collection of modules, stored in a directory with a special __init__.py file.
Example structure:
__init__.py makes Python treat the folder as a package.Python has a huge ecosystem of external packages.
You install them using pip (Python’s package manager).
Then use it in your code:
👉 Output:
✅ Math Calculations (math module):
✅ Random Password Generator (random module):
✅ Web API Request (requests package):
Try these in a new file (lesson12.py):
datetime module and print today’s date.random module to simulate rolling a dice (1–6).requests package to fetch data from an API.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).
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 MeYour email address will not be published. Required fields are marked *