Python Library Functions

Python Library Functions

Python contains a large library of standard functions which can be used for common programming tasks. You can also create your own.

पायथन में स्टैण्डर्ड फंक्शन की एक बड़ी लाइब्रेरी है जो सामान्य प्रोग्रामिंग कार्यों के लिए उपयोग की जा सकती है। आप भी अपना खुद का बना सकते हैं।

A function is just some Python code which is separated from the rest of the program. This has several advantages: Repeated sections of code can be re-used without rewriting them many times, making your program clearer. Furthermore, if a function is separated from the rest of the program it provides a conceptual separation for the person writing it, so they can concentrate on either the function, or the rest of the program.

एक फंक्शन सिर्फ कुछ पायथन कोड है जो बाकी कार्यक्रम से अलग है। इसके कई फायदे हैं: कोड के री-यूज्ड वर्गों को कई बार उन्हें फिर से लिखने के बिना उपयोग किया जा सकता है, जिससे आपका फंक्शन स्पष्ट हो जाता है। इसके अलावा, अगर कोई फंक्शन बाकी प्रोग्राम से अलग हो जाता है, तो यह लिखने वाले व्यक्ति के लिए एक वैचारिक अलगाव प्रदान करता है, इसलिए वे या तो फंक्शन या बाकी प्रोग्राम पर ध्यान केंद्रित कर सकते हैं।

Input ()

User enters the values in the Console and that value is then used in the program as it was required. To take input from the user we make use of a built-in function input ().

 यूजर कंसोल में वैल्यूज में प्रवेश करता है और उस वैल्यू को प्रोग्राम में तब उपयोग किया जाता है जैसा कि आवश्यक था। यूजर से इनपुट लेने के लिए हम एक बिल्ट-इन फंक्शन इनपुट () का उपयोग करते हैं।

input (): input() function can be used to read data directly from the keyboard in the form of string type.

इनपुट(): इनपुट) फंक्शन का उपयोग स्ट्रिंग के प्रकार के रूप में कीबोर्ड से सीधे डेटा पढ़ने के लिए किया जा सकता है।

How the input () function works in Python:
पायथन में इनपुट() फंक्शन कैसे काम करता है:

  • When input() function executes program flow will be stopped until the user has given an input.

जब इनपुट() फंक्शन निष्पादित होता है तो प्रोग्राम फ्लो को रोक दिया जाएगा जब तक कि उपयोगकर्ता ने इनपुट नहीं दिया हो।

  • Whatever you enter as input, input function convert it into a string. If you enter an integer value still input() function convert it into a string. You need to explicitly convert it into an integer in your code using typecasting.

आप जो भी इनपुट के रूप में दर्ज करते हैं, इनपुट फंक्शन उसे एक स्ट्रिंग में बदल देता है। यदि आप एक पूर्णांक मान अभी भी इनपुट() फंक्शन दर्ज करते हैं, इसे स्ट्रिंग में परिवर्तित करता है। आपको टाइपकास्टिंग का उपयोग करके इसे स्पष्ट रूप से अपने कोड में इंटीजर में बदलना होगा।

  • We have to convert that string type to our required type by using type casting method.

हमें टाइपिंग मेथड का उपयोग करके उस स्ट्रिंग टाइप को अपने आवश्यक टाईप में बदलना होगा।

For example:
>>> 
a = input ()
20
>>>a
‘20’
>>>print (a)
20
>>>type (a)
<class ‘str’> # type (a) is type of class ‘str’
# enter any float value
>>>val = input (“Enter a float value :”)
Enter a float value :44.34
>>>type (val)
<class ‘str’>
# enter Boolean value
>>>val = input (“Enter a Boolean value :”)
Enter a Boolean value : 44.34
>>>type (val)
<class ‘str’>

Example 1: Accept employee data from the user using the input function and displaying it using the print function.

इनपुट फंक्शन का उपयोग करके और प्रिंट फंक्शन का उपयोग करके उपयोगकर्ता से कर्मचारी डेटा स्वीकार करें।

name = input (“Enter Employee Name :”)
salary = input (“Enter salary :”)
company = input (“Enter Company name :”)
print(“*** Printing Employee Details ***”)
print (“Name”, “Salary”, “Company”)
print (name, salary, company)

Output:
Enter Employee Name :v k mishra
Enter salary :100000
Enter Company name : Gyancs
*** Printing Employee Details ***
Name Salary Company
v k mishra 100000 Gyancs

Whatever you enter as input, input function convert it into a string. If you enter an integer value still input() function convert it into a string. You need to do the explicit conversion into an integer in your code.

आप जो भी इनपुट के रूप में दर्ज करते हैं, इनपुट फंक्शन उसे एक स्ट्रिंग में बदल देता है। यदि आप एक इंटीजर वैल्यू अभी भी इनपुट() फंक्शन दर्ज करते हैं, इसे स्ट्रिंग में परिवर्तित करता है। आपको अपने कोड में एक इंटीजर में स्पष्ट रूपांतरण करने की आवश्यकता है।

Example 2: Program to check input type in Python?

पायथन में इनपुट टाईप की जांच करने का प्रोग्राम
number = input (“Enter number : “)
name = input (“Enter name ‘: “)
print (“Type of input values are …”)
print (“type of number”, type (number))
print (“type of name”, type (name))

Output:
Enter number : 100
Enter name : Gyancs
Type of input values are …
type of number <class ‘str’>
type of name <class ‘str’>

As we know whatever you enter as input, the input () function always converts it into a string. We need to convert an input value into an integer type explicitly. Let’s see the example to take integer input in python.

जैसा कि हम जानते हैं कि आप जो भी इनपुट के रूप में दर्ज करते हैं, इनपुट) फंक्शन हमेशा इसे एक स्ट्रिंग में परिवर्तित करता है। हमें इनपुट मान को इंटीजर टाईप में स्पष्ट रूप से परिवर्तित करने की आवश्यकता है। आइए पायथन में इंटीजर इनपुट लेने के लिए उदाहरण देखें

Example 3: The following code takes two input () from console and typecasts them to integer then prints the sum.

निम्न कोड कंसोल से दो इनपुट) लेता है और उन्हें इंटीजर में टाइप करता है फिर योग को प्रिंट करता है।
# input
num1 = input ( ‘enter a value : ‘)
num2 = input ( ‘enter b value : ‘)
# typecasting input values
num1 = int (num1)
num2 = int (num2)
# printing the sum in integer
print (num1 + num2)

Output:
enter a value : 100
enter b value : 200
300

Note: Print the above program in single line.
उपरोक्त प्रोग्राम को सिंगल लाइन में प्रिंट करें

print (int (input(‘enter a value : ‘)) + int (input(‘enter b value :’)))

Example 4: Write a program which will accept principle amount, time and rate of interest. Calculate the simple interest & total amount.

एक प्रोग्राम लिखें जो प्रिंसिपल एमाउन्ट, समय और इंट्रेस्ट की दर को स्वीकार करेगा। सिम्पल ब्याज और कुल राशि की गणना करें।

simpleIntrest.py
p = float (input (“Enter Principle amount : “))
t = float (input (“Enter Time period : “))
r = float (input (“Enter Rate of intrest: “))
si = (p * t * r) /100
totalAmount = p + si
print (“*”*30)
print(“Intrest on amount = “, si)
print(“Total Amount = “, totalAmount)
print (” * ” * 30)

Output:
Enter Principle amount : 50000
Enter Time period : 12
Enter Rate of intrest : 12
******************************
Intrest on amount = 72000.0
Total Amount = 122000.0
******************************

eval()

The eval() method parses the expression passed to it and runs python expressions code) within the program. In other word the function eval is used to evaluate the value of a string, see the example given below: eval()

मेथड उसके पास की गई एक्सप्रेशन को पास करती है और प्रोग्राम के भीतर पायथन एक्सप्रेशन (कोड) को चलाती है। दूसरे शब्द में एक स्ट्रिंग के मूल्य का मूल्यांकन करने के लिए फंक्शन eval() का उपयोग किया जाता है, नीचे दिए गए उदाहरण देखें:
Example:
>>> eval(“12”)
12
>>> eval(’12+12′)
24

Syntax – सिंटैक्स: eval(expression, globals=None, locals=None)

Parameter Description पैरामीटर डिस्क्रिप्शन

Expression      A String that will be evaluated as Python code

एक्सप्रेशन        एक स्टिंग, जिसका मूल्यांकन पायथन कोड के रूप में किया जाएगा

globals          Optional. A dictionary containing global parameters

ग्लोबल्स         ऑप्शनल। ग्लोबल पैरामीटर वाली एक डिक्शनरी

locals             Optional. A dictionary containing local parameters

लोकल्स          ऑप्शनल  एक डिक्शनरी जिसमें स्थानीय पैरामीटर होते हैं

Example 1:
expr = input (“Enter the Formula : “)
eval (expr)

The above function takes any expression in variable ‘expr’ as input. Then user has to enter a value of ‘expr’. Finally, we evaluate the python expression using eval() built-in function by passing the expr as argument.

उपरोक्त फंक्शन इनपुट के रूप में वैरियेबल ‘expr’ में किसी भी एक्सप्रेशन को लेता है। तब यूजर को ‘expr’ का वैल्यू दर्ज करना होगा। अंत में, हम expr को तर्क के रूप में पारित करके eval) बिल्ट-ईन फंक्शन का उपयोग करके. पायथन एक्सप्रेशन का मूल्यांकन करते हैं।

Output:
Enter the Formula :
print (“hello world”)
hello world

Example 2: Write a program using eval(), which variables and methods are available using dir() method. eval() का उपयोग करके एक प्रोग्राम लिखें, जो वैरियेबल और तरीके dir() विधि का उपयोग करके उपलब्ध हैं।

from math import *
print (eval(‘dir()’))

Output:
[‘__annotations__’, ‘__builtins__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘acos’, ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’, ‘comb’, ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘dist’, ‘e’, ‘erf’, ‘erfc’, ‘exp’, ‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘gamma’, ‘gcd’, ‘hypot’, ‘inf’, ‘isclose’, ‘isfinite’, ‘isinf’, ‘isnan’, ‘isqrt’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘log2’, ‘modf’, ‘nan’, ‘perm’, ‘pi’, ‘pow’, ‘prod’, ‘radians’, ‘remainder’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘tau’, ‘trunc’]

print()

The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

प्रिंट() फंक्शन निर्दिष्ट मैसेज को स्क्रीन, या अन्य मानक आउटपुट डिवाइस पर प्रिंट करता है। मैसेज एक स्ट्रिंग या कोई अन्य ऑब्जेक्ट हो सकता है, स्क्रीन पर लिखे जाने से पहले ऑब्जेक्ट स्ट्रिंग में परिवर्तित हो जाएगा।
Syntax: print (object (s), separator=separator, end=end, file=file, flush=flush)

Parameter  पैरामीटरDescription  डिस्क्रिप्शन
object(s)  Any object, and as many as you like. Will be converted to string before printed कोई भी ऑब्जेक्ट, और जितने चाहें। प्रिंट से पहले स्ट्रिंग में परिवर्तित हो जाएगा।
sep=’separator’Optional. Specify how to separate the objects, if there is more than one. Default is. ऑप्शनल। निर्दिष्ट करें कि ऑब्जेक्ट को कैसे अलग किया जाए, यदि एक से अधिक है।  डिफॉल्ट  है।
end=’end’Optional. Specify what to print at the end. Default is ‘\n’ (line feed) ऑप्शनल । निर्दिष्ट करें कि आखिर में क्या प्रिंट करना है। डिफॉल्ट ‘\n’ (लाईन फीड) है
fileOptional. An object with a write method. Default is sys.stdout ऑप्शनल। एक लेखन विधि के साथ एक ऑब्जेक्ट । डिफॉल्ट sys.stdout है।
flushOptional. A Boolean, specifying if the output is flushed (True) or buffered (False). Default is False ऑप्शनल । एक बूलियन, निर्दिष्ट करता है कि आउटपुट फ्लश किया गया है (टू) या बफर (फाल्स) । फाल्स डिफॉल्ट है।

Chapter wise Model Paper Link

About Me