Home >> Python Tutorial >> Python Modules
Edit Template
Edit Template

Python Modules in Hindi

What is Modules in Python

Modules एक ऐसे python files होते है जिसमे organized and reusable code लिखे होते है। किसी module में basically Classes, Functions, Variables, etc. runnable ही होते है, जिनको हम अपने program में import करके reuse कर सकते है।

Pre-defined Modules

Python में कई सारे pre_defined modules होते है जिनको हम simply अपने program में import करके use कर सकते है, जैसे-

math

इस module में बहुत से mathematical functions होते है।

ramdom

इस module का use करके हम random numbers को generate कर सकते है।

datetime

इस module का use date और time के manipulation में किया जाता है।

Creating a Modules

जैसा की हमने अभी discus किया कि Modules के python file ही होते है इसलिए module को create करने के लिए हमे सबसे पहले एक python file create करना होगा फिर हम उसमे code को लिखेंगे, हम जैसा चाहे वैसा code लिखा सकते है जैसे कि अगर हमे कुछ functions को अपने program में बार-बार use करना है तो हम उन सभी functions की coding इस module लिख सकते है फिर हम इस module को अपने किसी भी program में import करके इसमे लिखे सभी functions को access कर सकते है।

🐍
Example 1.py
Copy to clipboard
# my_module.py

def greet(name):
    return f"Hello, {name}"

def add(a, b):
    return a + b

How to Import a Module

किसी भी module को किसी दूसरे module या फिर किसी script (program) में import करने के लिए “import” statement का use करते है।

हम चाहे तो एक module के किसी specific item को भी import कर सकते है।

🐍
Example 2.py
Copy to clipboard
import my_module

print(my_module.greet("Alice"))  # Output: Hello, Alice
print(my_module.add(5, 3))       # Output: 8

# Importing specific item
from my_module import greet, add

print(greet("Bob"))   # Output: Hello, Bob
print(add(2, 4))      # Output: 6

Aliasing

कोई module को import करने के बाद उसके items को use करने के लिए हमे उस module के नाम को लिखना होता है but किसी module का नाम हमे बड़ा लग रहा है और हम चाहते है की उसे एक छोटे स nickname देकर उस module और उसके items को use करे तो हम aliasing करते है, इसके लिए हमे सिर्फ “as” statement का use करना होगा।

🐍
Example 3.py
Copy to clipboard
import my_module as mm

print(mm.greet("Charlie"))  # Output: Hello, Charlie
print(mm.add(10, 20))       # Output: 30

from my_module import greet as g

print(g("Dave"))  # Output: Hello, Dave

उम्मीद करते है कि आपको Python Modules अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
File Pointer

Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods      2.1. tell() Method      2.2....

File Handling

Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...

Python Modules

Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules      2.1. math      2.2. random...

Python Iterators

Table of Contents All Chapters 1. Iterators      1.1. Iterable      1.2. Iterator      1.3. Iterator Protocol...

Python Polymorphism

Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism      3.1. Polymorphism...

Python Inheritance

Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance      3.1. Single Inheritance...

Python Function

Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...

Python Dictionary

Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...

Edit Template
Scroll to Top