Library Functions in Python in Hindi – लाइब्रेरी फंक्शन्स क्या होते हैं?
Table of Contents
- 1. Library Functions in Python in Hindi – लाइब्रेरी फंक्शन्स क्या होते हैं?
- 2. Introduction to Built-in Functions – बिल्ट-इन फंक्शन्स का परिचय
- 3. Commonly Used Functions (input(), eval(), print()) in Python in Hindi
- 4. FAQs
जब भी हम Python में कोई program लिखना शुरू करते हैं, तो हमें बहुत सारे छोटे-छोटे काम बार-बार करने पड़ते हैं — जैसे screen पर कुछ print करना, user से input लेना या किसी list की length निकालना। अगर इन सब कामों के लिए हमें हर बार खुद से code लिखना पड़े, तो programming बहुत मुश्किल और time-consuming हो जाएगी। यहीं पर Library Functions काम आते हैं।
Library Functions वो ready-made functions होते हैं जो Python के अंदर पहले से ही मौजूद होते हैं, ताकि programmer को common tasks के लिए बार-बार code लिखने की जरूरत न पड़े।
इन्हें Built-in Functions भी कहा जाता है, क्योंकि ये Python के core system में built (built-in) होते हैं और बिना किसी extra import के directly इस्तेमाल किए जा सकते हैं।
Library Functions का सबसे बड़ा फायदा यह है कि ये पहले से ही test किए हुए, optimized और reliable होते हैं, इसलिए इनका इस्तेमाल करने से program की speed और accuracy दोनों बेहतर होती है।
Beginners के लिए यह समझना जरूरी है कि Python में दो तरह के functions होते हैं — एक जो हम खुद बनाते हैं (user-defined functions), और दूसरे जो Python पहले से provide करता है (library या built-in functions)।
Exams और coding interviews में भी अक्सर पूछा जाता है कि कुछ common built-in functions कैसे काम करते हैं, इसलिए इन्हें practically समझना बहुत जरूरी है।
SVG Diagram: Library Functions की Categories
यह diagram दिखाता है कि Python के functions मुख्यतः दो categories में बंटे होते हैं — User-Defined Functions (जो हम खुद लिखते हैं) और Library / Built-in Functions (जो Python पहले से provide करता है), जिनमें print() और input() जैसे functions शामिल हैं।
Introduction to Built-in Functions – बिल्ट-इन फंक्शन्स का परिचय
Built-in Functions Python language का एक core हिस्सा हैं। ये functions Python interpreter के साथ पहले से ही install होकर आते हैं, इसलिए इन्हें इस्तेमाल करने के लिए किसी अलग module को import करने की जरूरत नहीं पड़ती। Python में लगभग 68 से ज्यादा built-in functions होते हैं, जिनमें से कुछ का इस्तेमाल हम लगभग हर program में करते हैं।
Built-in Functions को directly किसी भी Python file में बिना import किए इस्तेमाल किया जा सकता है — जैसे
len(),type(),print()वगैरह।वहीं कुछ अन्य library functions ऐसे भी होते हैं जो अलग-अलग modules के अंदर होते हैं, जैसे
mathmodule मेंsqrt()याrandommodule मेंrandint()— इन्हें इस्तेमाल करने से पहले उस module को import करना पड़ता है।Built-in Functions programmer का बहुत सारा समय बचाते हैं, क्योंकि किसी basic operation के लिए बार-बार लंबा code लिखने की जरूरत नहीं पड़ती।
चूंकि ये functions Python team द्वारा पहले से ही test किए जा चुके होते हैं, इसलिए इनमें bugs की संभावना बहुत कम होती है और performance भी बेहतर होती है।
नीचे दिए गए example में हम कुछ common built-in functions का इस्तेमाल एक साथ देख सकते हैं:
numbers = [12, 45, 7, 89, 34]
print(len(numbers)) # List की length बताता है
print(max(numbers)) # सबसे बड़ी value बताता है
print(min(numbers)) # सबसे छोटी value बताता है
print(sum(numbers)) # सभी numbers का sum बताता है
print(sorted(numbers)) # List को sorted order में देता है
print(type(numbers)) # Data type बताता है
ऊपर दिए गए example में len(), max(), min(), sum(), sorted() और type() — ये सभी built-in functions हैं, जिन्हें हमने बिना कोई import किए directly इस्तेमाल किया है। यही Built-in Functions की सबसे बड़ी खूबी है — ये हमेशा ready होते हैं।
Commonly Used Functions (input(), eval(), print()) in Python in Hindi
अब बात करते हैं Python के तीन सबसे ज्यादा इस्तेमाल होने वाले built-in functions की — print(), input() और eval()। ये तीनों functions लगभग हर beginner program में देखने को मिलते हैं, इसलिए इन्हें अच्छे से समझना बहुत जरूरी है।
1. print() Function
print() function का इस्तेमाल किसी भी value, message या variable को screen पर दिखाने के लिए किया जाता है। यह Python का सबसे ज्यादा इस्तेमाल होने वाला function है।
name = "Anjali"
age = 21
print("Hello, welcome to Python!")
print(name)
print("Age:", age)
print(f"{name} is {age} years old.")
ऊपर दिए गए example में हमने print() को अलग-अलग तरीकों से इस्तेमाल किया है — सीधा message print करना, variable print करना, comma से multiple values print करना, और f-string की मदद से values को message के अंदर जोड़ना।
print()function में हमsepऔरendजैसे parameters भी इस्तेमाल कर सकते हैं, जो output के format को control करते हैं।By default
print()के हर call के बाद एक नई line आ जाती है, लेकिनend=""देकर हम इसे रोक भी सकते हैं।
print("Hello", "World", sep="-") # Output: Hello-World
print("Loading", end="...")
print("Done")
# Output: Loading...Done
2. input() Function
input() function का इस्तेमाल user से keyboard के जरिए data लेने के लिए किया जाता है। यह जो भी value लेता है, उसे हमेशा string के रूप में return करता है, चाहे user ने number ही क्यों न डाला हो।
name = input("अपना नाम डालें: ")
print("Hello,", name)
age = input("अपनी उम्र डालें: ")
print(type(age)) # Output:
ध्यान देने वाली बात यह है कि अगर हमें number की तरह calculation करनी हो, तो input() से मिली value को हमें manually int() या float() में convert करना पड़ता है।
age = int(input("अपनी उम्र डालें: "))
next_year_age = age + 1
print("अगले साल आपकी उम्र होगी:", next_year_age)
3. eval() Function
eval() function किसी string के अंदर लिखी हुई Python expression को directly execute करके उसका result return करता है। यह function अक्सर input() के साथ इस्तेमाल किया जाता है, ताकि user से number या expression सीधे सही data type में मिल सके।
expression = "5 + 3 * 2"
result = eval(expression)
print(result) # Output: 11
eval() को अक्सर calculator जैसे programs में इस्तेमाल किया जाता है, जहाँ user directly कोई expression डालता है:
num1 = eval(input("पहला number डालें: "))
num2 = eval(input("दूसरा number डालें: "))
print("Sum:", num1 + num2)
इस example में अगर user 5 डालेगा, तो eval() उसे directly integer 5 में convert कर देगा, न कि string "5" में — जो कि input() अकेले करता। यही कारण है कि बहुत सारे programs में eval(input()) का combination इस्तेमाल किया जाता है।
हालांकि
eval()को इस्तेमाल करते वक्त सावधानी भी बरतनी चाहिए, क्योंकि यह किसी भी valid Python expression को execute कर देता है, इसलिए untrusted input के साथ इसे इस्तेमाल करना risky हो सकता है।Beginners के practice programs और छोटे calculators के लिए
eval()बहुत useful है, लेकिन बड़े real-world applications में इसकी जगह सुरक्षित तरीकों (जैसे सीधाint()याfloat()conversion) का इस्तेमाल किया जाता है।
SVG Diagram: print(), input() और eval() का Flow
यह diagram दिखाता है कि कैसे input(), eval() और print() एक साथ मिलकर काम करते हैं — पहले user से data लिया जाता है, फिर उसे calculate किया जाता है, और अंत में result screen पर print किया जाता है।
| Function | काम | Return Type |
|---|---|---|
| print() | Screen पर output दिखाना | None |
| input() | User से data लेना | हमेशा String |
| eval() | String expression को calculate करना | Expression के according (int, float, आदि) |