
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Modules एक ऐसे python files होते है जिसमे organized and reusable code लिखे होते है। किसी module में basically Classes, Functions, Variables, etc. runnable ही होते है, जिनको हम अपने program में import करके reuse कर सकते है।
Python में कई सारे pre_defined modules होते है जिनको हम simply अपने program में import करके use कर सकते है, जैसे-
इस module में बहुत से mathematical functions होते है।
इस module का use करके हम random numbers को generate कर सकते है।
इस module का use date और time के manipulation में किया जाता है।
जैसा की हमने अभी discus किया कि Modules के python file ही होते है इसलिए module को create करने के लिए हमे सबसे पहले एक python file create करना होगा फिर हम उसमे code को लिखेंगे, हम जैसा चाहे वैसा code लिखा सकते है जैसे कि अगर हमे कुछ functions को अपने program में बार-बार use करना है तो हम उन सभी functions की coding इस module लिख सकते है फिर हम इस module को अपने किसी भी program में import करके इसमे लिखे सभी functions को access कर सकते है।
# my_module.py
def greet(name):
return f"Hello, {name}"
def add(a, b):
return a + b
किसी भी module को किसी दूसरे module या फिर किसी script (program) में import करने के लिए “import” statement का use करते है।
हम चाहे तो एक module के किसी specific item को भी import कर सकते है।
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
कोई module को import करने के बाद उसके items को use करने के लिए हमे उस module के नाम को लिखना होता है but किसी module का नाम हमे बड़ा लग रहा है और हम चाहते है की उसे एक छोटे स nickname देकर उस module और उसके items को use करे तो हम aliasing करते है, इसके लिए हमे सिर्फ “as” statement का use करना होगा।
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 करे,
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...
Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules 2.1. math 2.2. random...
Table of Contents All Chapters 1. Iterators 1.1. Iterable 1.2. Iterator 1.3. Iterator Protocol...
Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism 3.1. Polymorphism...
Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance 3.1. Single Inheritance...
Table of Contents All Chapters 1. Classes and Objects 2. Class in Python 3. Class Structure 3.1. Attributes...
Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...
Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...