Modules क्या होते है ?
Modules Python language मे बहुत महत्वपूर्ण भूमिका निभाते है | Python के अन्दर modules बनाई गई एक file होती है | इस File का नाम कुछ भी हो सकता है | इस file के अन्दर classes , function , methods etc. create किए जाते है | इस file मे बनाए गए functions , classes को एक अलग file मे import किया जा सकता है | जैसे पिछले chapter मे आपने function create किए और वे function उसी file मे create किए जिस file मे आप programs लिख रहे थे | पर modules के उपयोग के से आप यही Functions किसी और file मे लिख करके और जिस file मे coding कर रहे है | उस file मे import कर सकते है | modules के उपयोग से बडे - बडे programs को लिखने मे आसानी होती है | और modules की file को .py की extension से save किया जाता है | module file मे बनाए गए function classes etc. को अलग file मे import करने के लिए import statement का उपयोग किया जाता है |
import statement क्या होती है ?
किसी एक file मे बनाए गए functions , classes variable etc. को किसी अलग File मे import करने के लिए import statement का उपयोग किया जाता है | एक file मे किसी दूसरी file को import करने के बहुत तरीके है | नीचे एक example.py नाम की file बनाई गई है | जिसके functions को try.py मे import किया गया है |
1def sum(a, b): 2 print(a+b) 3 4def substraction(x, y): 5 print(x - y) 6 7def multiplication(l, m): 8 print(x * y)
Syntax Explanation
def keyword के use से functions को create किया गया है | फिर functions के नाम define किए गए है | फिर parameters paas किए गए है | इन parameters मे वो value store होगी | जो user देना चाहेगा | अब इस file की सहायता से एक एक करके import statement के सभी methods का निम्नलिखित वर्णन किया गया है |
1. Simple Import (Method)
किसी File के सभी functions , classes , variables etc. को import करने के लिए simple Import method का उपयोग किया जाता है | इस method से पूरी की पूरी file import की होती है | simple import method मे सब कुछ file के नाम से access होता है | जिससे की code clean होता है | जैसे :-
1import example 2sum = example.sum 3sum(5 ,2)
7
Syntax Explanation
जिस file को import करना है | import लिख करके उस file का नाम लिख दे | फिर जिस भी function classes variable का
use करना है | उस function का नाम लिख करके Use करे |
NOTE : import statement के उपयोग से केवल functions ही import नही किए जाते है | कोई class , variable ,
program etc. import किए जा सकते है |
2. from … import … (Method)
किसी file से किसी special function , class variable etc. को import करने के लिए from import method का उपयोग किया जाता है | जैसे :-
1from example import sum 2sum(5, 20)
25
Syntax Explanation
fromfrom word सीधे example.py को target कर रहा है | मतलब file को target करता है |
example example python file का नाम है |
import import word sum नाम के function को import कर रहा है |
sum function का नाम है | जिसका Use करना है | और जिसको example.py मे create किया गया है |
(5 , 20) यही वो values है | जो sum function के parameters मे store होगी | जो parameters है (a , b)
3. Alias (method)
तो आप चाहते है कि मे उस function का पूरा नाम न देकर short मे नाम दू | तो ऐसा करने के लिए Alias Method का उपयोग किया जाता है | जैसे :-
1from example import sum as s 2s(10, 30)
40
Syntax Explanation
as लगाने के बाद दिया गाए item का नाम short मे लिखा जा सकता है |
4. File Alias (Method)
जब file के नाम को भी short मे लिखना हो यानि alias बनाना हो तब File Alias Method का उपयोग किया जाता है | जैसे :-
1import example as ex 2ex.sum(5, 20)
25
Syntax Explanation
as लगाने के बाद file का भी name short मे लिखा जा सकता है |
Types OF Modules
Python के अन्दर Modules को तीन भाग मे devide किया गया है |
Built-in-Modules क्या होते है ?
Python के अंदर बहुत सारे modules पहले से ही मौजूद होते हैं जिन्हें built-in modules कहा जाता है। जो Python के साथ आते है | मतलब इन Modules को अलग से install करने की जरूरत नही होती है | कुछ Built-in-Modules के उदाहरण है :- os, sys, math, datetime etc. python| कुछ Built-in-Modules का नीचे वर्णन किया गया है |
1. Math Module
math module एक Built-In module है | जिसका उपयोग mahtematical calculations के लिए किया जाता है |Square root , Power , Trigonometry etc. काम करने के लिए math module का उपयोग किया जाता है | math module मे कुछ functions है | जिनके जरिए ये काम किए जा सकते है |
1import math 2 3print(math.factorial(8)) 4print(math.sqrt(25)) 5print(math.floor(7.4))
40320
5.0
7
Syntax Explanation
1. import math math module को import करने के लिए math module लिखा गया है |
2. print(math.factorial(8)) factorial function का उपयोग किसी number का factorial निकालने के
लिए किया जाता है | math.factorial() मतलब
math module से factorial function जिस number का factorial निकालना है | उस number को() brackets मे लिखा जाता है |
3. sqrt() किसी number का Square root निकालने के लिए sqrt() function का उपयोग किया जाता है |
4. floor() किसी decimal number की decimal value को हटा देता है | decimal value यानि
. के बाद वाली value
2. Random Module
Random Module का उपयोग कोई Random number generate करने , किसी list को shuffle करने etc. काम करने के लिए किया जाता है | यही नही games बनाने मे भी Random module का उपयोग किया जाता है | ये काम करने के लिए Random module मे भी कुछ functions होते है | जिनके जरिए ये काम किए जा सकते है |
1import random 2 3print(random.randint(10, 100)) 4 5cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 6random.shuffle(cards) 7print(cards) 8 9animals = ["tiger", "cat", "dog"] 10print(random.choice(animals))
74
[9, 10, 2, 4, 6, 3, 7, 5, 1, 8]
tiger
Syntax Explanation
1. import random random module को import किया गया है | फिर
2. randint किसी rondom number को generate करने के लिए random randint function का उपयोग किया जाता
है | फिर
3. print(random.randint(10, 100)) random module के randint function से 10 से ले कर 100 तक
के बीच कोई एक number print करो | फिर
4. shuffle numbers को shuffle करने के लिए shuffle function का use किया जाता
है | फिर
5. random.shuffle(cards) random.shuffle(cards) मतलब random module के shuffle function से cards variable मे store numbers को
shuffle करो |
6. choice() दिए गए names मे से किसी एक name को print choose करने के लिए choice
function का उपयोग किया जाता है | फिर
7. print(random.choice(animals)) मतलब print करो , random module के choice function
से animals variable मे stored names मे से किसी एक को |
3. Date Time Module
Days , Months , Year से related काम करने के लिए भी Python मे एक module है | जिसका नाम Date Time Module है | इस module का उपयोग करके current time से related projects पर काम किया जाता है | Date Time module मे भी कुछ functions होते है | ये functions है जैसे :-
1. strftime("%b")
current month name को short name मे print करने के लिए strftime("%b") function का उपयोग किया जाता है |
1import datetime 2 3q = datetime.datetime.now() 4n = q.strftime("%b") 5print(n)
Nov
Syntax Explanation
import datetime datetime module को import किया गया है | फिर
q = datetime.datetime.now() q नाम का variable create किया गया है | जिसमे
datetime module से now time जैसे day , Month etc. को store किया गया है | फिर
n = q.strftime("%b") n नाम का variable create किया गया है | लिखा गया है कि
q मे store value मे से strftime("%b") function की help से short मे Month name को n variable मे store करो | फिर
print(n) अब n variable मे store value को print करने के लिए print function का use किया गया है |
2. %B
full Month name को पता करने के लिए %B का उपयोग किया जाता है |
3. %m
numbers मे month name पता करने के लिए %m का उपयोग किया जाता है |
4. %y
year के last के 2 numbers को मे पता करने के लिए %y का उपयोग किया जाता है |
5. %Y
year को numbers मे पता करने के लिए %Y का उपयोग किया जाता है |
6. %T
current Time को पता करने के लिए %T का उपयोग किया जाता है |
User-defined Module क्या होते है ?
user defin modules वे modules होते है | जिनको user अपने काम को सरल बनाने हेतु create करता है | यानि इन modules को user के द्वारा बनाया जाता है | ये module python की एक file होती है | जिसके अन्दर user functions , classes , varriables etc. create करता है | और इस file से इन functions varriables classes etc. को किसी और file के अन्दर import करता है |
User-Defined Module बनाने के steps
step 1 :example.py नाम की एक File बनाओ |
step 2 :example.py मे एक function बनाए जैसे :-
# example.py 1def sum(a , b) 2 print(a + b)
step 3 : अब एक try.py नाम की file बनाए |
step 4 : अब try.py मे sum function के use का code लिखे |
# try.py 1import example 2sum = example.sum 2sum(5 ,2)
इस तरह से आप User-Defined Modules का उपयोग कर सकते है |
User-Defined Modules के लाभ
1. Reusability
modules का तो खास उपयोग इसलिए किया जाता है कि किसी code को बार - बार repeat ना करना पडे |
मतलब modules के उपयोग से एक ही कोड बार-बार उपयोग कर सकते हैं |
2. Readability
Modules के उपयोग से किसी error का सामना नही करना पडता है | यानि कोड साफ और समझने योग्य बनता है |
3. Time Saving
कोई code को बार - बार repeat न करने के कारण time की बचत होती है |
4. speed
Modules के उपयोग से user की code लिखने और समझने मे speed बडती है |
5. Team Work Friendly
अलग-अलग लोग अपने code के अनुसार अलग - अलग modules बना सकते हैं
3. Third-Party Modules
ये वे modules होते है | जो न तो python के साथ में पहले से install हुए आते है | और न ही user के द्वारा बनाया जाते है | बल्कि ये modules किसी बाहरी organization या किसी बाहरी developer के द्वारा बनाऐ जाते है | इसलिए इन modules का use करने के लिए इन्हें अलग से install करना होता है | इन modules को install करने के लिए pip command का use किया जाता है |
For Example
1pip install module-name 2# For Example 2pip install emoji
How To Use Of emoji module
1import emoji 2print(emoji.emojize("I love python : snake:"))
advantages of Third-Party Modules
1. TIme Saving इन modules को user नही बनाता , इसलिए user के time बचत होती है |
user इनको install करके use कर सकता है |
2. Performance Because ये Modules बाहरी organization से बनाए जाते है |
और इनको c , c++ जैसी languages मे बनाया जाता है , इसलिए इन Modules की Performance बहुत तेज होती है |
3. New Features Third-Party Modules मे features updates add time to time add किए जाते रहते है |
4.Extendable Projects आप आपने projects के अनुसार Third-Party Modules को modify
कर सकते है |
5. Complex Projects Third-Party Modules बडे projects पर काम करने के लिए helpful होते है |
Third-Party modules की आवश्यकता क्यों होती है ?
built-in modules और एक शुरूआती programmer की कुछ limits होती है | इसलिए Third-Party modules
की आवश्यकता होती है | नीचे Third-Party modules की आवश्यकताओ का वर्णन किया गया है |
1. Python Built-In Modules limits Python मे Built-In Moduls की कुछ limits होती है | जैसे :-
की built-in modules सामान्य काम कर सकते हैं |
2. Speed user की speed मे growth करने के लिए |
3. Technology Features अब के समय मे हर दिन कोई न कोई नया feature आता है |
इसलिए built-in modules मे हर दिन नए features add नही किए जाते |
4. Interest कुछ programmers की Modules बनाने मे रूचि नही होती है , इसलिए
वो programmers modules को install करके काम करते है |
5. No Knowledge कुछ begginers programmers अपने projects के अनुसार functions classes
create नही कर पाते इसलिए वो Third-Party Modules को install कर लेते है |
अध्याय समाप्त — आगे बढ़ें
आपने इस chapter मे python के बहुत ही महत्वपूर्ण concepts समझे है जो आगे की प्रोग्रामिंग सीखने मे बहुत Helpful हैं। आगे के chapter मे आप python string के बारे मे सिखेगे आगे के chapter की और बढ़ने के लिए नीचे दिए गए बटन पर क्लिक करे |
Next Chapter
Seekho Coding