3 Python Modules
1. What is a Module?
A module is a file that contains Python code.
In simple words:
👉 A module is a Python file (.py)
👉 It has functions, variables, or classes
👉 We use it to reuse code
2. Why Use Modules?
Modules help us to:
-
Organize code
-
Reuse code
-
Keep program clean
-
Avoid writing same code again
3. Using a Built-in Module
Python has many built-in modules.
Example: math Module
4. Import Only What You Need
Example
5. Import with Alias
Example
6. Create Your Own Module
Step 1: Create a File mymodule.py
Step 2: Use the Module
7. Import Specific Function from Module
Example
8. The dir() Function
|
0 1 2 3 4 5 6 |
dir() shows all names in a module. |
Example
9. Built-in Modules Examples
Random Module
Date and Time Module
10. Common Beginner Mistakes
❌ Wrong Module Name
❌ Error.
❌ File Name Conflict
👉 Do not name your file same as module.
11. Simple Practice Examples
Example 1: Use math module
Example 2: Create and Use Module
Example 3: Alias Import
12. Summary (Python Modules)
✔ Module is a Python file
✔ Used to reuse code
✔ Built-in & user-defined modules
✔ Import full or part of module
✔ Makes program organized
📘 Perfect for Beginner eBook
This chapter is ideal for:
-
Python beginner books
-
School & college students
-
Self-learners
If you want next, I can write:
-
Packages
-
Virtual Environment
-
Exception Handling
-
File Handling
-
Mini Python Projects
Just tell me 😊
