Function Function vs Module vs Library:
1. A group of lines with some name is called a function
2. A group of functions saved to a file , is called Module
3. A group of Modules is nothing but Library
Types of Variable
1.Global Variable
2.Local Variable
1.Global Variable:
A variable declared outside the function is called global variable.
It is available to all functions available in module
2. Local variable
A variable declared inside the function is called a local variable.
It is available only in the function not outside the function
glo_var = 10 #global Variable
def fun():
loc_var = 20 #local Variable
print(glo_var)
print(loc_var)
print(glo_var)
#print(loc_var) #name 'loc_var' is not defined
Global Keyword
To declare global variable inside the function
To access global variable inside the function (Loc and Glo variable name are same)
1.
def fun1():
global glo_variable
glo_variable = 40
print(glo_variable)
def fun2():
print(glo_variable)
fun1()
fun2()
#print(glo_variable)#global variable available to other function but in module
#2.
a = 22
def fun3():
a = 44
print(a)#loc
print(globals()['a'])
fun3()
Recursive Functions
A function call itself is called recursive function
#print list item without loop
def print_num(items):
if len(items) > 0:
print(items[0])
items.pop(0)
print_num(items)
print_num([11,33,77,99,66])
Anonymous OR Lambda Functions:
A function without a name is called an Anonymous function.
We can define Anonymous OR Lambda function using lambda keyword
By using this function we can able to write concise code so code readability is improved
Syntax:
lambda argument_list : expression
WAP to get full-name of user
full_name = lambda first_Name,last_Name : first_Name + ' ' + last_Name
print(full_name("Chaitanya","Patil"))
#WAP to find large number from two number
large_Num = lambda num1,num2 : num1 if num1 > num2 else num2
print(large_Num(23,56))
filter(), map() and reduce()
filter()
Used to filter a sequence based on condition.
filter(function,sequence)
WAP to find even num from list
Without lambda
def isEven(num1):
if num1%2 == 0:
return True
else:
return False
lists = [44,12,77,66,89,97]
print(list(filter(isEven,lists)))
With lambda
print(list(filter(lambda x:x%2==0,lists)))
map()
used to perform same operation on every element that time we use map.
we can used two list at a time but list length must be same
map(fun,seq)
WAP to get square of every item
without lambda
def square(num):
return num * num
print(list(map(square,lists)))
with lambda
print(list(map(lambda n:n*n,lists)))
reduce()
used to reduce a sequence of elements into single elements.
reduce() function present in the functools module and hence we should write an import statement.
with lambda
from functools import *
print(reduce(lambda m,n:m*n,lists))
Function Aliasing:
For existing function we can given another name is called function aliasing
def sum(num1,num2):
return num1 + num2
sum2 = sum
print(id(sum))
print(id(sum2))
print(sum2(11,22))
Nested Function
we are able to declare another function within the function
def fun1():
print("Fun1")
def func2():
print("Fun2")
fun1()
#fun2()