KEMBAR78
Recursion and Lambda Functions in python.ppt
RECURSION AND LAMBDA FUNCTIONS
VINAY AGRAWAL
ASST. PROFESSOR
CEA DEPT.
GLA UNIVERSITY,
MATHURA
Recursion
Tower of Hanoi Problem
def toh(source,destination,aux,n):
if n>=1:
toh(source,aux,destination,n-1)
print("Move",source,"to",destination)
toh(aux,destination,source,n-1)
toh('A','B','C',3)
Programs
1. WAP to find the factorial of a number provided
by user using Recursion.
2. WAP to print the fibonacci Series upto n terms,
where n is provided by user.
3. WAP to find the GCD (or HCF) using Recursion.
4. WAP to input a string from the user and check
whether it is palindrome or not.
Lambda Functions or Anonymous Functions
 Those Functions which does not have any name is called
Anonymous Functions. In Python, this can be achieved by using
keyword lambda, that’s why it is also called Lambda Functions.
 They are not declared in the standard manner by using the def
keyword
 Lambda forms can take any number of arguments but return
just one value in the form of an expression. They cannot
contain commands or multiple expressions.
Syntax
 The syntax of lambda functions contains only a single
statement, which is as follows:
lambda arg1 ,arg2,.....argn:expression
Example:
add = lambda a, b : a + b
print("Value of total : ", add( 10, 20 ) )
Non-Anonymous Function Anonymous Function
def square(x):
return x*x
n=int(input(“Enter the Number”))
print(square(n))
a=lambda x:x*x
n=int(input(“Enter the Number”))
print(a(n))
Lambda Function with map() function
l=list(map(int,input().split()))
Similarly
L1=[1,2,3,4]
L2=list(map(lambda x:x*x,L1))
print(L2)
Output:[1,4,9,16]
Lambda Function with filter() function
L1=[1,2,3,4]
L2=list(filter(lambda x:x%2==0,L1))
print(L2)
Output:[2,4]
Lambda Function with reduce() function
import functools
L1=[1,2,3,4,5,6,7,8,9,10]
L2=functools.reduce((lambda a , b : a+b), L1)
print(L2)
Output:55

Recursion and Lambda Functions in python.ppt

  • 1.
    RECURSION AND LAMBDAFUNCTIONS VINAY AGRAWAL ASST. PROFESSOR CEA DEPT. GLA UNIVERSITY, MATHURA
  • 2.
  • 3.
  • 5.
  • 6.
    Programs 1. WAP tofind the factorial of a number provided by user using Recursion. 2. WAP to print the fibonacci Series upto n terms, where n is provided by user. 3. WAP to find the GCD (or HCF) using Recursion. 4. WAP to input a string from the user and check whether it is palindrome or not.
  • 7.
    Lambda Functions orAnonymous Functions  Those Functions which does not have any name is called Anonymous Functions. In Python, this can be achieved by using keyword lambda, that’s why it is also called Lambda Functions.  They are not declared in the standard manner by using the def keyword  Lambda forms can take any number of arguments but return just one value in the form of an expression. They cannot contain commands or multiple expressions.
  • 8.
    Syntax  The syntaxof lambda functions contains only a single statement, which is as follows: lambda arg1 ,arg2,.....argn:expression Example: add = lambda a, b : a + b print("Value of total : ", add( 10, 20 ) )
  • 9.
    Non-Anonymous Function AnonymousFunction def square(x): return x*x n=int(input(“Enter the Number”)) print(square(n)) a=lambda x:x*x n=int(input(“Enter the Number”)) print(a(n))
  • 10.
    Lambda Function withmap() function l=list(map(int,input().split())) Similarly L1=[1,2,3,4] L2=list(map(lambda x:x*x,L1)) print(L2) Output:[1,4,9,16]
  • 11.
    Lambda Function withfilter() function L1=[1,2,3,4] L2=list(filter(lambda x:x%2==0,L1)) print(L2) Output:[2,4]
  • 12.
    Lambda Function withreduce() function import functools L1=[1,2,3,4,5,6,7,8,9,10] L2=functools.reduce((lambda a , b : a+b), L1) print(L2) Output:55