18MIT14C-U2
18MIT14C-U2
• Once the function is created, this function can be called anytime to perform
that task.
• Each function is given a name. A function may or may not return a value.
4. dir() function
• dir() takes an object as an argument
Prepared by S.Radha Priya 6
• It returns a list of strings which are names of members of that object
Ex:
import math
list=dir(math)
Print list
Output
*‘cos’, ’sin’, ’tan’, ’log’, ’pi’, ’pow’……+
6. help() function
• It is a built in function which is used to invoke the help system.
• It gives all the detailed information about the module.
Ex:
import math
help(math.sin) #gives the detailed information about the sin function
help(math.cos)
Ex:
def display()
print(“welcome to python coding”)
display() #call function
Output
Welcome to python coding.
• Keyword arguments
• Default arguments
Required arguments
When we assign the parameters to a function at the time of function
definition, at the time of calling, the arguments should be passed to a
function in correct positional order.
Ex:
def display(name,age)
Print(“name=“,name,”age=“,age)
display(“radha”,21)
Display(21,”ramya”) #it passes 21 to name and ramya to age
For these arguments we use (*) before the name of the variable, which holds
the value of all non-keyword variable arguments.
>>>def info(arg1,*vartuple):
print “result is”,arg1
for var in vartuple:
print var
return
>>>info(10); >>>info(90,60,40);
Output output
Result is 10 result is 90
60
40
Ex:Factorial of a number
4!=4*3*2*1=24
Prepared by S.Radha Priya 20
>>>def fact(x):
if x==1:
return 1
else:
return(x*fact(x-1))
>>>fact(4)
Output 24
Fibonacci numbers 1,1,2,3,5,8…….
Def fib(n):
if n==0:
return 1
if n==1:
return 1
return fib(n-1)+fib(n-1)
Print(“the value of fibonacci numbers”, fib(8))