Dextries
Dextries
Day-1
Function:
->It is the group of statements
-> the purpose of the function is . we can use the function for multiple times.
->There are two types of functions
-> In built function : pythons has built the own inbuilt function
Ex: type(),input()
->user defined function : the functions which are developed by the programmer
*While developing the function we use def keyword
Ex: syntax DEF function_name(parameters):
return object_name
->Function using without parameters
Syntax: def f_name():
Print(‘Hii’) # function defination
f_name() # function calling
-> Function without parameter and return statement
Syntax : def fun_name():
Print(return ‘hii’/object_name)
->function with using parameters
Synatx : def fun_name(obj):
Print(‘Hii’)
Fun_name(obj)
->Function with parameter and return statement
Syntax : def fun_name(obj)
Print(return ‘hii’)
fun_name(obj)
def muk(a,b):
sum=a+b
sub=a-b
return sum,sub
print(muk(10,20))
Keyword argument:
->we can pass argument values by keyword
def key(user_name,Phno):
print(user_name,'\n',Phno)
key(user_name='Mukrsh*gmail.com',Phno=2345678)
key(Phno=12345678,user_name='[email protected]')
In this example keyword means user_name,Phno
Default arguments:
->we can pass default arguments in the function definition
def df(emotion='hii'):
print(emotion)
df('hello') # o/p hello
df() # o/p hii if we did not pass any value default value is hii willl print
->
Types of variable : Local & Global Variable
Global variable :
->Global variable is the variable is declared outside the function
->that variable can access all the functions
a=10
def dis():
print(a)
def play():
print(a)
dis()
play()
Local variable:
->The variable is declared inside the function
def dis():
a=10
print(a)
def play():
print(a)
dis() # 10
play() # error
Global keyword
->The global keyword is used to convert the local variable into global .
-> We should declare the global variable inside the function.
a=100
def dis():
global a
a=200
print(a)
def paly():
print(a)
dis()
paly()
->If the Global variable and the local variable having the same name. We can access
the global variable in side the function.
a=300
def dis():
a=200
print(a)
print(globals()['a'])
def paly():
print(a)
dis() o/p 200 local variable , 300 ->global variable value
paly() o/p 300 g_v
Nested function: