Python Mastery – From Basics to Brilliance / Modules & Packages

Importing Modules & Creating Your Own

📦 What is a Module?

A module is a Python file that contains functions, variables, or classes — allowing you to reuse code across multiple programs.


🔧 Importing Built-in Modules

Python comes with many built-in modules.

import math
print(math.sqrt(16)) # 4.0

🧩 Creating Your Own Module

File: mymodule.py

def greet(name):
    return f"Hi {name}!"

✅ Note: Do not include function calls in the module file if you plan to import it elsewhere.


📥 Using the Custom Module in Another File

File: main.py

import mymodule
print(mymodule.greet("Ravi"))
Hi Ravi!

💡 Real-life Analogy:

Think of a module like a toolkit. Instead of rebuilding a screwdriver every time, you import the one that’s already made.