🐍 Day 12 – Modules and Packages in Python

 

In the last blog, we explored list comprehension — a clean and compact way to write loops in one line.

Today, let’s take one step ahead and learn about Modules and Packages in Python. These help us organize code better and reuse it smartly, especially in bigger projects.





πŸ“¦ What is a Module?

A module in Python is just a file that contains Python code.
You can define functions, classes, or variables inside it — and then reuse that code in another file.

Imagine writing your utility code once and using it anywhere you want — that’s what modules are for.

✨ Built-in Modules

Python comes with many built-in modules that you can start using right away. Just import them using the import keyword.

A few useful built-in modules:

  • math → for math operations

  • random → for generating random values

  • datetime → for working with dates & times

  • os → for interacting with the operating system

  • sys → for system-specific parameters


πŸ› ️ Creating Your Own Module

Let’s say you have a file named greetings.py:

                             


Now, in another file:

                                        


This is how you create and use your own module in Python.

πŸ“ What is a Package?

A package is just a folder that contains multiple Python modules.

It helps organize related modules together.

To make Python treat a folder as a package, you add a special file inside it:

__init__.py

🧱 Folder Structure Example

                    


Now you can import modules from this package like:
                    

🧠 Why Use Modules & Packages?

  • Keeps your code organized

  • Makes code reusable

  • Helps in working with bigger projects

  • Easy to maintain and test

πŸ” Recap

Module → A single .py file with reusable code

Built-in modules like math, random, etc.

✅ You can create your own modules

Package → A folder with related modules + __init__.py

✅ Helps structure large codebases


🎯 Practice Task

Try this:

  • Create a folder called mytools. Inside that, create two files:
  • math_tools.py → add functions like add(), subtract()
  • string_tools.py → add functions like upper_case(), lower_case()
  • Then create a third file and import both modules to use the functions.


πŸ‘‹ Wrapping Up

That’s all for today!
Tomorrow, we’ll dive into File Handling in Python — how to read, write, and work with files on your system. πŸ“„✍️

Keep coding, stay patient, and enjoy the process πŸ’»✨


Comments

Popular posts from this blog

Welcome to Python Fun Zone: Learn, Code, Build!

Day 1: Getting Started with Python – A Friendly Introduction for Beginners

Day 2: Variables and Data Types – The Building Blocks of Python 🐍