
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Function एक reusable blocks of code है जिसका use किसी specific task को perform करने के लिए किया जाता है। जब भी हमे अपने program मे ऐसे block of code की जरूरत पड़ती है जिसका use पूरे program मे कई बार होने वाला होता है तो उस code का function बना लेते है जिससे हमे उस code को बार बार लिखने की जरूरत नहीं होती हम बस उस function को call करेंगे हमारा काम हो जाएगा ।
Def: Def keyword का use करके हम function define करते है,
Function name: किसी function को identify करने के लिए उसका एक unique name होता है जिसे हम function name कहते है ।
Function Body: इसमे function का वो code होता है जो किसी specific task perform करता है।
SYNTEX
# function
def l_name():
return "Singh"
name = "Ritika"
print(name, l_name()) # function calling
# Output: Ritika Singh
किसी variable को program में कहा तक access किया जा सकता है उसे ही scope उस variable का scope कहते है। Python में mainly 2 types के scope होते है-
अगर हम function के अंदर कोई variable बनाते है तो उसका use सिर्फ उसी function मे ही हो सकता है इसे ही local scope कहा जाता है, और ऐसे variables को local variable कहते कहते है।
ऐसे variables जो function body या किसी भी block के बाहर बनाते है उन्हे global variables कहा जाता है, इन variables को पूरे program मे कही भी use किया जा सकता है, और उस variable के scope को global scope कहेंगे।
a = "global"
def function1():
a = "local"
print(a)
function1()
print(a)
# Output: local
# global
अगर हम चाहते है कि function के अंदर जो variable है उसका use कही भी कर सके तो उसके लिए global keyword का use करना होगा
a = "global"
def function1():
global a
a = "local"
print(a)
function1()
print(a)
# Output: local
# local
जब हम कोई function create करते है तो वो by default “none” return करता है, but अगर हम function से किसी value को return करना चाहते है तो return keyword का use करके एक या एक से अधिक values को return करा सकते है। function जो भी value return करेगा वो function calling के जगह पर आ जाएगा।
Note : जब function के अंदर का code चलता हो तो return मिलते है function का execution complete हो जाता है अगर हमने function body में return line के नीचे कोई code लिखा है तो वो execute नहीं होगा, इसलिए return को हमेशा last में लिखा जाता है but किसी specific condition पर हम return को बीच में भी use करते है।
def add(a, b):
return a + b
result = add(5, 7)
print("Sum is:", result)
# Output: Sum is: 12
Aliasing का मतलब होता है किसी function को दो अलग-अलग नाम से use करना ऐसा हम तब करते है जब किसी function नाम hard होता है तो हम एक simple नाम में उसे बदल देते है। aliasing करने के लिए हमे बस एक variable को create करना है और उसमे function name को assign कर देना अब हम उस variable को as a function use कर सके है।
def greet():
print("Hello Python")
say_hello = greet # alias ban gaya
say_hello() # greet() ke barabar hoga
# Output: Hello Python
अगर हम function को define करके उसके function body मे कोई code न लिखे तो यह error देगा । error से बचने के लिए हम pass का use करते है.
SYNTEX
उम्मीद करते है कि आपको Python Function अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers 2.1. Public Members 2.2. Protected Members...
Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...
Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...
Table of Contents All Chapters 1. Parameters and Aguments 2. Function Parameters 2.1. *args 2.2. **kwargs...
Table of Contents All Chapters 1. Context Manager 2. with Statement 2.1. __enter__() Method 2.2. __exit__()...
Table of Contents All Chapters 1. Error Handling 2. Types of Error 2.1. Syntax Error 2.2....
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...