LAB 5 : Functions in Python
LAB 5 : Functions in Python
Functions are reusable pieces of programs. They allow you to give a name to a block of statements allowing you to run that block using the specified name anywhere in your program and any number of times. This is know as calling function.
def printthis(): print('Welcome to RSTForum') printthis()
Welcome to RSTForum
Above was an example of simple function without arguments.Lets see an example where we can provide an argument to the function
def printthis(string): print(string) printthis('Hi All') printthis('Welcome to RSTforum') printthis('Best institute for python training')
Hi All Welcome to RSTforum Best institute for python training
Positional Argument in Python
Positional arguments are arguments that need to be included in the proper position or order. The first positional argument always needs to be listed first when the function is called
def addit(a,b): z=a+b print(z) addit(100,2345) addit(412,456)
Default Arguments in python
They allow you to give default value to your parameters. Default arguments can be given only to the last variable def fuc(a,b=5) is correct but def func(a=5,b) is wrong.
def say(string,times=2): print(string*times) say('hello') say('Bye',5)
Python allows functions to be called using keyword arguments. When we call functions in this way, the order (position) of the arguments can be changed.
def func(a=1,b=4,c=3): print('a is:',a,'b is:',b,'c is:',c) func(c=4,b=6,a=1)
a is: 1 b is: 6 c is: 4
Variable Arguments in Python
Sometimes you might want to define a function that can take any number of parameters, i.e. variable/multiple number of arguments , this can be achieved using the variable arguments
def variab(a,*number,**phonebook): print('a',a) for i in number: print('number',i) for name,extn in phonebook.items(): print(name,extn) variab(10,11,3,5,paul=1000,harry=1001,richard=1101)
10 11 3 5 paul 1000 harry 1001 richard 1101
Namespace and scope resolution
x=50 def fun(): x =2 print('value of x inside the func is',x) fun() print('value of x outside the func is',x)
value of x inside the func is 2 value of x outside the func is 50
x=50 def fun(): global x x =2 print('value of x inside the func is',x) fun() print('value of x outside the func is',x)
value of x inside the func is 2 value of x outside the func is 2
The return statement is used to exit or break out of the function. We can optionally return a value from the function as well. Function always returns none value if we have not mentioned what value it has to return. Every function has a return none statement explicitly at the end .
def say(): print('Hello') return 10 print(say())
def square(x): return x**2 print(square(9))
Example : To find average percentage of two students
def percentage(x): if x<650: marks=x/650 percent=marks*100 return percent else: print('Invalid marks') peter=percentage(500) print('Peter has scored',peter) john=percentage(640) print('John has scored',john) average = (peter + john)/2 print("The average percentage of both is",average)
Peter has scored 76.92307692307693 John has scored 98.46153846153847 The average percentage of both is 87.69230769230771
Lambda() function in Python
Lambda function is a way to create small anonymous functions, i.e. functions without a name. These functions are throw-away functions, i.e. they are just needed where they have been created.
add=lambda x : 20+x add(10)
Lambda() function with filter() function
Lamda with filter() , filter () works on a pre defined condition. IN below example filter function will provide a list of numbers which are >5 in our given list.You cannot perform operation like division, multiplication , addition etc while using filter()
list1=[1,3,4,5,6,8,9,12] fil=filter(lambda x:x>5,list1) print(list(fil))
[6, 8, 9, 12]
Lambda() Function with map() function
Lambda with map() , map () function is used to apply a particular operation like division, multiplication, addition etc.
x=[1,3,4,5,6,8,9,12] fil=map(lambda x:x*5,x) print( list(fil))
[5, 15, 20, 25, 30, 40, 45, 60]
Lambda function with reduce() function
Lambda with reduce(), reduce() like map(), is used to apply an operation to every element in a sequence. However, it differs from the map in its working.
- Perform the defined operation on the first 2 elements of the sequence.
- Save this result.
- Perform the operation with the saved result and the next element in the sequence
- Repeat until no more elements are left.
from functools import reduce fil=reduce(lambda x,y:x+y,x) print(fil)
Generator function and generator expression in Python
Generator functions look and act just like regular functions, but with one defining characteristic. Generator functions use the Python yield keyword instead of return.
def func(x): for i in x: yield(i*i) if x.index(i)==2: print('index value 2 encountered') li=func([10,20,30,40,50]) print(li) print(next(li)) print(next(li)) print(next(li)) print(next(li)) print(next(li))
<generator object func at 0x000001DC29DAB410> 100 400 900 index value 2 encountered 1600 2500
when next() is called on a generator object (either explicitly or implicitly within a for loop), the previously yielded variable is incremented, and then yielded again.
Nested Function in python
def gen(): x=2 print(x,'this is the outher func') def g(): print(x,'this is inner') g() gen()
2 this is the outher func 2 this is inner