हेल्लो दोस्तों ! आज इस पोस्ट में python के बारे में बताया गया है की Python Functions in hindi क्या होता है कैसे काम करता है और भी बहुत कुछ दिया गया है तो चलिए शुरू करते है
Contents
Introduction to Python Functions
हािाँकक python में functions create करना दूसरी ज्यादातर programming languages से अलग है, लेकिन यदि python आपकी first programming language नहीीं है और आप पहिे ककसी दूसरी programming language में programming कर चुके है तो आप python functions को आसानी से define और use कर पाएींगे।
Block of Statements
यदि python आपकी first programming language है और आप functions के बारे में पहिी बार पढ़ रहे है तो में आपको बताना चाहता है हूँ की एक function ककसी program के अींदर कई statements का एक block होता है।
ये statements लमिकर ककसी specific task को perform करते है। Function block का एक नाम होता है जो की पुरे program में unique होता है। इसी नाम के द्वारा इस function में लिखे गए सभी statements को एक साथ execute किया जाता है।
No Need to Redefine a Code
यदि आपके program में कोई ऐसा code है जिससे आप बार बार program में कई जगह पर execute करना चाहते है तो इसके लिए आप उस code को function में define कर सकते है। इसके बाद आप उस function को उसके नाम के द्वारा कई जगह पर call (execute) करवा सकते है।
Functions को use करने से आपको उस code को बार बार लिखने की जरुरत नहीीं होती है। इससे program की redability बढ़ती है, आपका time बिता है और memory space भी बिता है।
Defined Using def Keyword
Python में function def keyword द्वारा define किया जाता है।
<def> <function-name>(parameters-list) :
<function body>
Python में def keyword के बाद function का नाम define किया जाता है।
Function के नाम के बाद parameters की list define की जाती है।
Function Body with Indentation
Next line से function की body शुरू होती है। जैसा की आपको पता है की python में spaces का अिग महत्व है। Function की body को शुरू करने से पूवय tab-space (indentation) आता है। यह space बताता है की वह statement function की body में आता है।
Function को define करने के बाद आप जजतने भी statements function body में define करना चाहते है उनको indentation के साथ लिखते है। जब आप कोई statement बबना indentation के लिखते है तो वँही पर function का end मान लिया जाता है।
Function Documentation String or Docstring
Python functions में आप documentation string define कर सकते है। यह documentation string function के बारे में जानकारी देती है। इसे docstring भी कहा जाता है।
यह function की पहिी line में define की जाती है। Docstring को define करना optional होता है।
def myFunction():
a normal function as usual. //Docstring
यदि आप multiline docstring define करना चाहते है तो उसे आप triple quotes में define करते है।
“”” This is
a multiline docstring”””
Returning Value
यदि आप python function में से कोई value return नहीीं करते है तो ऐसे में python द्वारा automatically None string return की जाती है। इसे आप print() में ऐसे function को execute करके देख सकते है जो कुछ भी value return नहीीं करता है।
Local Symbol Table of Python Functions
जब एक function को execute होता है तो python द्वारा automatically एक symbol table create की जाती है। इस symbol table को local symbol table कहा जाता है।
Stores Local Variables of a Function
Local symbol table function में define ककये गए local variables को store करती है। Variables names और उनको assign की गयी values इस table में store की जाती है।
Function को pass ककये जाने वािे arguments local symbol table में लसिय तब ही store ककये जाते है जब function call होता है। Python functions में arguments call by value method से pass ककये जाते है और वे हमेशा एक object reference होते है।
यदि कोई function ककसी दूसरे function को call करता है तो उस situation में एक नयी symbol table create की जाती है।
Function Body Represented Using Function Name
Local symbol table में function की body function के नाम के द्वारा represent की जाती है। यानी की ककसी function की body को उसके नाम की value के रूप में store किया जाता है।
Python में function body का भी एक certain type होता है जो लसिय interpreter द्वारा identify किया जाता है। जब local symbol में function की body function के नाम की value के रूप में store की जाती है तो python interpreter उसे identify कर िेता है।
इसके बाद उस value को (function-body) को ककसी दूसरे नाम (function name) को भी assign किया जा सकता है। ऐसा होने पर वह function भी original function की तरह ही कार्य करेगा।
आसान शब्दों में कहा जाये तो python में function body एक value के रूप में treat की जाती है और उसे ककसी भी दूसरे function name को assign किया जा सकता है।
Python Function Arguments
Arguments वे values होती है जो function को call करते समय pass की जाती है। Python में आप मुख्यतः दो प्रकार के arguments use कर सकते है।
Default Arguments
Python आपको parameters की default value set करने की ability provide करती है। आप ककसी parameter की value को एक default value provide करते है और वह value तब use की जाती है जब function को call करते समय उस parameter के लिए कोई value नहीीं pass की जाती है।
Default arguments parameters को assignment operator द्वारा set ककये जाते है।
def hello_Func(name=’User’): print(‘hello’+name)
ऊपर दिए गए function को call करते समय यदि कोई argument नहीीं pass किया जाता है तो default argument use किया जाता है।
Keyword Arguments
Python में एक special type के arguments और introduce ककये गए है जजन्हें keyword arguments कहा जाता है। Python आपको key/value pair के रूप में arguments pass करने की ability provide करती है।
इसमें parameter को key के रूप में और argument को उसकी value के रूप में pass किया जाता है।
def myFunction(name): print(‘hello’+name)
myFunction(name=’Javahindi.com’)
निवेदन:-आप सभी छात्र –छात्रों से निवेदन है की अगर आपको ये Topic(Python Functions in hindi) अच्छा लगा हो तो कृपया आप इस वेबसाइट के बारे में अपने दोस्तों को जरुर बताये अगर कोई topic(Python Functions in hindi) से संबधित प्रश्न हो तो कमेंट्स(comments) आपके लिए ही बना है और किसी Subject के लेकर भी कोई प्रश्न हो तो कमेंट करे