Home >> Python Tutorial >> Python Function
Edit Template
Edit Template

Python dictionary in Hindi

What is Python Function

Function एक reusable blocks of code है जिसका use किसी specific task  को perform करने के लिए किया जाता है। जब भी हमे अपने program मे ऐसे block of code की जरूरत पड़ती है जिसका use पूरे program मे कई बार होने वाला होता है तो उस code का function बना लेते है जिससे हमे उस code को बार बार लिखने की जरूरत नहीं होती हम बस उस function को call करेंगे हमारा काम हो जाएगा ।

How to Create Function

Def: Def keyword का use  करके हम function define करते है,

Function name: किसी function को identify करने के लिए उसका एक unique name होता है जिसे हम function name कहते है । 

Function Body: इसमे function का वो code होता है जो किसी specific task perform करता है।

SYNTEX

def function_name( ):
       # Function body
       # return
🐍
example 1.py
Copy to clipboard
# function
def l_name():
    return "Singh"

name = "Ritika"
print(name, l_name()) # function calling

# Output: Ritika Singh

Scope in Function

किसी variable को program में कहा तक access किया जा सकता है उसे ही scope उस variable का scope कहते है। Python में mainly 2 types के scope होते है-

Local Scope and Local Variable

अगर हम function के अंदर कोई variable बनाते है तो उसका use सिर्फ उसी function मे ही हो सकता है इसे ही local scope कहा जाता है, और ऐसे variables को local variable कहते कहते है। 

Global Scope and Global Variable

ऐसे variables जो function body या किसी भी block के बाहर बनाते है उन्हे global variables कहा जाता है, इन variables को पूरे program मे कही भी use किया जा सकता है, और उस variable के scope को global scope कहेंगे।  

🐍
example 2.py
Copy to clipboard
a = "global"

def function1():
    a = "local"
    print(a)

function1()
print(a)
# Output: local
#         global

Global Keyword

अगर हम चाहते है कि function के अंदर जो variable है उसका use कही भी कर सके तो उसके लिए global keyword का use करना होगा

🐍
example 3.py
Copy to clipboard
a = "global"

def function1():
    global a
    a = "local"
    print(a)

function1()
print(a)
# Output: local
#         local

Function Return

जब हम कोई 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 करते है।

🐍
Example 4.py
Copy to clipboard
def add(a, b):
    return a + b

result = add(5, 7)
print("Sum is:", result)

# Output: Sum is: 12

Aliasing Function

Aliasing का मतलब होता है किसी function को दो अलग-अलग नाम से use करना ऐसा हम तब करते है जब किसी function नाम hard होता है तो हम एक simple नाम में उसे बदल देते है। aliasing करने के लिए हमे बस एक variable को create करना है और उसमे function name को assign कर देना अब हम उस variable को as a function use कर सके है।

🐍
Example 5.py
Copy to clipboard
def greet():
    print("Hello Python")

say_hello = greet   # alias ban gaya
say_hello()         # greet() ke barabar hoga

# Output: Hello Python

Pass Statement

अगर हम function को define करके उसके function body मे कोई code न लिखे तो यह error देगा । error से बचने के लिए हम pass का use करते है.

SYNTEX

def function_name():
    pass

उम्मीद करते है कि आपको Python Function अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
Dunder Methods

Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods   Python Tutorial Python Introduction Identation &...

Encapsulation

Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers      2.1. Public Members      2.2. Protected Members...

Lambda Function

Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...

Nested Function

Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...

Context Manager

Table of Contents All Chapters 1. Context Manager 2. with Statement      2.1. __enter__() Method      2.2. __exit__()...

File Pointer

Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods      2.1. tell() Method      2.2....

File Handling

Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...

Edit Template
Scroll to Top