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)
Note : Returning multiple values fro m the function
In other languages(c,c++,java) we can not access multiple values in the function
def muk(a,b):
sum=a+b
mul=a*b
return sum,mul
print(muk(10,20))
Types of arguments:
Positional arguments
Keyword arguments
Default arguments
Variable length arguments
Positional argument
-> The n.o of arguments and positional arguments should be same
Here positional arguments means passing the parameters while calling the function
->If the change the no of the parameter are different.it will get error
-> if the order is change. It will change the result
Ex: def muk(a,b):
sum=a+b
mul=a*b
return sum,mul
print(muk(10,20,30)) # Error by (change in n.o of parameters
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
->Note we can use both positional,keyword arguments in the function calling.
But we need to follow the order 1st positional and then keyword argument
def key(user_name,Phno):
print(user_name,'\n',Phno)
key('
[email protected]',123456789) #Valid
key('
[email protected]',Phno=2345678) #Valid
key(user_name='
[email protected]',345678) #Error Due to srguments are not in the order.
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
->Difference between the function and modules and package
1) Function is the group of statements with a name
2) Modules is the group of functions
3) Package group of modules
Recursive function :
->The function calling itself is known as recursive function
->Purpose of recrsive is:
>we can reduce the length of the code and increase the readability.
>we can solve the complex problem easily.
def factorial(n):
if n==0:
return 1
else:
return n*factorial(n-1)
print(factorial(5))
Anonymous Function/ Lambda function
->the function without having the name / nameless function
->we can use the function for instance use(one time use)
-> Syntax : lambda argument_list : Expression
#Normal function
def sqrt(n):
return n*n
print(sqrt(4))
#Lambda function
s=lambda n : n*n
print(s(4))
Nested function: