Functions in Python_250426_235532
Functions in Python_250426_235532
TYPES OF FUNCTIONS
Functions can be categorized into three types:
1) Built in Functions.
2) Modules.
3) User Defined functions.
1) BUILT IN FUNCTIONS
These are predefined function in python and are used as and when there is need
by simply calling them. For example:
int()
float()
str()
min()
max() ...etc
for example:
from random import randint
3) USER DEFINED FUNCIONS
Function is a set of statements that performs specific task.
Example:
def sum_diff(x,y):
add=x+y
def main():
x=9
y=3
a,b=sum_diff(x,y)
print("Sum = ",a)
print("diff = ",b)
main()
Python Lambda
A lambda function is a small anonymous function which can take any number of
arguments, but can only have one expression.
E.g.
x = lambda a, b : a * b
print(x(5, 6))
OUTPUT: 30
def sum_diff(p,q):
add=p+q
diff=p-q
return add,diff
Arguments are the values which are passed while calling a function
Mrinmoy Paul (PGT-CS/IP)
def main():
x=9
y=3
a,b=sum_diff(x,y)
print("Sum = ",a)
print("diff = ",b)
main()
TYPES OF ARGUMENTS
Python supports following type of arguments:
1. Positional arguments
2. Default Arguments
3. Keyword Arguments
4. Variable Length Arguments
1. POSITIONAL ARGUMENTS
These are the arguments passed to a function in correct positional order
For example:
def substract(a,b):
print(a-b)
>>>substract(100,200)
-100
>>>substract(150,100)
50
2. DEFAULT ARGUMENTS
When a function call is made without arguments, the function has defalut values
for it for example:
For example:
Mrinmoy Paul (PGT-CS/IP)
def greet_msg(name=“Mohan”):
print(“Hello “, name)
greet_msg()
greet_msg(“Vinay”) # valid
greet_msg() #valid
3. KEYWORD ARGUMENTS
If function containing many arguments, and we wish to specify some among
them, then value for such parameter can be provided by using the name.
For example:
def greet_msg(name,msg):
print(name,msg)
greet_msg()
#calling function
greet_msg(name=“Mohan”,msg=“Hi”): # valid
O/p -> Mohan Hi
def main():
print(“Input integers”)
a=input()
a=a.split()
for i in range(len(a) ):
a[i]=int(a[i])
avg=list_ave(a)
print (“Average is = “, avg)
def main():
x=9
y=3
a,b=add_diff(x,y)
print("Sum = ",a)
print("diff = ",b)
print("m in main function = ",m)
main()