- Python Basics
-
Overview
- Introduction to Python and Installation
- Variables and Data Types
- Conditional Statements (if-else)
- Loops (for, while)
- Functions and Lambda Expressions
- Lists, Tuples, and Dictionaries
- File Handling (Reading/Writing Files)
- Exception Handling (Try, Except)
- Modules and Packages
- List Comprehensions and Generators
Modules and Packages
Add to Bookmark1. Introduction
Python modules and packages allow you to organize code into reusable components, making it easier to manage large projects.
- A module is a single Python file containing functions, classes, or variables.
- A package is a collection of modules organized in directories with an
__init__.py
file.
2. Modules in Python
A module is just a .py
file that contains Python code (functions, variables, classes, etc.).
Creating and Importing a Module
1. Create a file math_operations.py
with the following code:
# math_operations.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
2. Now, import and use the module in another Python file:
import math_operations
result = math_operations.add(5, 3)
print("Addition:", result)
The math_operations
module is imported and used.
3. Importing Specific Functions
Instead of importing the entire module, you can import specific functions.
from math_operations import add
print("Sum:", add(4, 6))
Only the add
function is imported.
4. Using Aliases for Modules
You can rename modules using as
.
import math_operations as mo
print("Subtraction:", mo.subtract(10, 4))
5. Built-in Modules in Python
Python includes many built-in modules.
Examples:
import math
import random
print(math.sqrt(16)) # 4.0
print(random.randint(1, 10)) # Random number between 1 and 10
The math
and random
modules provide ready-to-use functions.
6. Packages in Python
A package is a directory containing multiple modules, along with an __init__.py
file.
Creating a Package Structure
my_package/
│── __init__.py
│── module1.py
│── module2.py
1. Create module1.py
inside my_package
:
def greet(name):
return f"Hello, {name}!"
2. Import and Use the Package:
from my_package import module1
print(module1.greet("Ankit"))
The function from module1.py
is used.
7. Installing and Using External Packages
You can install third-party packages using pip
.
Example: Installing NumPy
pip install numpy
Using an Installed Package
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
numpy
is installed and used.
8. Summary
A module is a Python file containing code.
A package is a collection of modules inside a directory with an __init__.py
file.
Use import
to bring modules into your program.
Use pip
to install third-party packages.
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- How Multimodal Generative AI Will Change Content Creation Forever
- How to Start Your Career as a DevOps Engineer
- AI in Marketing & Advertising: The Future of AI-Driven Strategies
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- What is YII? and How to Install it?
- What Is SEO and Why Is It Important?
- The Ultimate Guide to Starting a Career in Computer Vision
- 10 Awesome Data Science Blogs To Check Out
- Python Challenging Programming Exercises Part 1
- Datasets for Speech Recognition Analysis
- Role of Digital Marketing Services to Uplift Online business of Company and Beat Its Competitors
- Deep Learning (DL): The Core of Modern AI
- The Beginner’s Guide to Normalization and Denormalization in Databases
- Downlaod Youtube Video in Any Format Using Python Pytube Library
- Navigating AI Careers in 2025: Data Science, Machine Learning, Deep Learning, and More
Datasets for Machine Learning
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset