Function ek block of code hota hai jo ki us code se related operation perform krta hai. Python mai kafi saare built in function hai jaise print(), input(), file(), int() etc , pr aap yaha khud ka function bhi create kr skte ho jise ham user - defined function kahenge.
Function ko create krne ke do steps hai pehla hai defining a function or dusra hai Calling the function.
Python function ko hm define krte hai def keyword ki madad se.
Syntax:
#Defining a function -step 1
def name(parameter1, parameter2,....parameterN):
Statement
return
#Callling a function - step 2
name()
Function ki jrurt kyo padi ?
Function basically duplication of code ko reduce krta hai , jaise agar maan lo ki apko kuch block of code ki jrurt hai, aur usi program mai apko us block of code ki doobara jrurt hai , to apko baar baar wahi code doobara likhna padega , iski bajaye ap ye code function ke andar likhiye aur jb bhi jrurt pade use call kr lijeye , isse apko wahi code doobara nhi likhna padega.
Aayiye ise example ki madad se aur clear krte hai.
----------------------------------------------------------------------------------
#Step 1 - Defining a function
def rishabh():
print("self love is defination of self discipine")
print("Ok Thankyou")
#Step 2 - Calling a function
rishabh()
Output:
self love is defination of self discipine
Ok Thankyou
#hme jb bhi is block of code ki jrurt padegi to hme sirf rishabh() ko
sirf call krne ki jrurt hai , hme ye block of code doobara likhne ki koi jrurt nhi hai.
#yaha function name rishabh hai, function ko hm define krte hai def keyword ki madad se
#indentation ka proper dhyan dena hai
----------------------------------------------------------------------------------
def add(x,y):
z=x+y
print(z)
add(12,4)
Output:
16
# x aur y arguments hai ,arguments function ke baad parenthesis
ke andar likhe jate hai.
#yaha hmne 12 aur 4 value pass ki hai argument mai,aur function
ko call kiya hai.
--------------------------------------------------------------
def update(x):
x=5
print(x)
update(7)Output:5# Notice the things
-----------------------------------------------------------
# Use of return keyword
# Example
def pro(x,y): z=x+y print(z)
pro(5,6)
Output:
11
But if
pro(5,6)+6
Output:
In [ ]:
#mtlb hm ye operation nhi kr skte pr hm ye operation return
keyword ki madad se kr skte hai, kaise? let's see
def hello(a,b):
c=a+b
return c
hello(10,3)
Output:
13But ifhello(10,3)+4
Output:
17# Yaha pr hme error nhi mili aur ye operation complete ho paya# Return keyword hamare function ke result ko save kr letahai#Isi vajaye se hme koi error nhi mili , function ka result save ho jata hai to hm uske sath ko bhi operation kr skte hai.-------------------------------------------------------------Local variable and global variableLocal variable aise variable hote hai jise hm sirf functionke andar hi excess kr skte hai.Global variable ko hm code mai kahi bhi excess kr skte hai,kahi bhi mtlb function ke andar bhi aur function ke bahar bhiexcess kr skte hai.Aayiye ise example ki madad se aur clear krte hai ki local variable aur global variable kya hai.---------------------------------------------------------def rishabh(): a =10 #local variable print(a)
rishabh()
Ouput:
10# Yaha pr a local variablebut ifprint(a)Output:Error#Hm dekh skte hai ki jb hmne local variable a ko outside thefunction excess krne ki koshish ki , to hme error mil gyi.#So local variable ko hm function ke bahar access nhi kr skte-------------------------------------------------------------a =10 # global variable def something(): print(a)
something()
Output:
10#Yaha pr a global variable hai kyoki a yaha pr outside the function hai.But ifprint(a)Output:10#to ap yaha dekh skte hai ki global variable ko hm yaha inside the function or outside the function bhi excess kr skte hai----------------------------------------------------------# Another casev =12 # Global variable def fuck(): v=14 # local variable print(v)
fuck()
Output:
14# Jb global aur local variable dono present ho to preferencehmesha local variable ko hi milti hai.but ifprint(v)Output:12#output mai hme 12 mila kyoki hmne outside the function v ko access kiya# pr agar hm chahte hai ki hm global variable ko function ke andar excess kr paye aur us global variable ki value function ke andar change kr paye to ye bhi hm kr skte hai global keyword ki madad se.#Let's see-------------------------------------------------------------h =23 def door(): global h h=12 print(h)
door()
Output:
12#Inside the function hme output 12 mila#but ifprint(h)Output:12#Pr ap dekh skte hai ki outside the function bhi output hame 12 mila#Iska mtlb ye hua ki hm global variable ki value inside the function change kr skte hai global keyword ki madad se------------------------------------------------------------Lambda functionLambda function is one line function, ise hm anonymous function bhi kahte hai kyoki lambda function ke name nhi hote.Let's see an example:---------------------------------------------------------# Normal functiondef power2(x): return x ** 2
power2(5)
Output:
25# x**2 - read as x ki power 2#Lambda functiona = lambda x : x ** 2
a(5)
Output:
25# Ap yaha dekh skte hai ki lambda function one line function hota hai , lambda function ka koi name nhi hota#Lambda function ek object ki tarah bhi behave kr skta hai , jaisa ki ap dekh skte hai ki kaise hmne lambda function ka result ek variable a mai store kr diya ab is a ko hm kahi bhi excess kr skte hai------------------------------------------------------------#Normal functiondef power2(x): return (x ** 2, x**3, x**4)
power2(5)
Output:
(25, 125, 625)#Lambda functionraj = lambda x : (x ** 2, x**3, x**4)
raj(5)
Output:
(25, 125, 625)---------------------------------------------------------------------------------------# Other examples:bharath = lambda x,y : (x ** 2, y**3, x**4)
bharath(2,3)
Output:
(4, 27, 16)--------------------------------------------------------------------------------------sum = lambda x, y, z : x+y+z sum(1,2,3)
Output:
6--------------------------------------------------------------------------------------
Comments
Post a Comment