computer scince and engineeringpython

python Anonymous functions (Lambda Function) Recursive Functions with examples in hindi

28views

Anonymous functions (Lambda Function)

•Recursive Functions with examples

Lambda Function

•Anonymous function is a function that is defined without a name.

•Anonymous functions are defined using the lambda keyword.

•Therefore, anonymous functions are also called lambda functions.

•The object returned by lambda is usually assigned to a variable or used as a part of other bigger functions.

•Instead of the conventional def keyword used for creating functions, a lambda function is defined by using the lambda keyword. 

Syntax:

  lambda arguments: expression

•This function can have any number of arguments but only one expression, which is evaluated and returned.

•One is free to use lambda functions wherever function objects are required.

•You need to keep in your knowledge that lambda functions are syntactically restricted to a single expression.

•It has various uses in particular fields of programming besides other types of expressions in functions.

Example 1 of Lambda Function

#calculate square of value

  f=lambda x:x*x

  value=f(5)

  print(‘Square of 5= ‘,value)

#calculate sum of two numbers

  f=lambda x,y:x+y

  result=f(1.55,10)

  print(‘sum=’, result)

Example 2 of Lambda Function

lambda function that doubles the input value.

# Program to show the use of lambda functions

double = lambda x: x * 2

print(double(5))

Run Code

Output: 10

In the above program, lambda x: x * 2 is the lambda function. Here x is the argument and x * 2 is the expression that gets evaluated and returned.

This function has no name. It returns a function object which is assigned to the identifier double. We can now call it as a normal function. The statement

double = lambda x: x * 2

is nearly the same as:

def double(x):

   return x * 2

Use of Lambda Function in python

We use lambda functions when we require a nameless function for a short period of time.

In Python, we generally use it as an argument to a higher-order function (a function that takes in other functions as arguments). Lambda functions are used along with built-in functions like filter(), map() etc.

Using Lambdas with filter () Function

  • The filter() function in Python takes in a function and a list as arguments.

The function is called with all the items in the list and a new list is returned which contains items for which the function evaluates to True.

Here is an example use of filter() function to filter out only even numbers from a list.

# Program to filter out only the even items from a list

my_list = [1, 5, 4, 6, 8, 11, 3, 12]

new_list = list(filter(lambda x: (x%2 == 0) , my_list))

print(new_list)

Run Code

Output

[4, 6, 8, 12]

Example use with map()

The map() function in Python takes in a function and a list.

The function is called with all the items in the list and a new list is returned which contains items returned by that function for each item.

Here is an example use of map() function to double all the items in a list.

# Program to double each item in a list using map()

my_list = [1, 5, 4, 6, 8, 11, 3, 12]

new_list = list(map(lambda x: x * 2 , my_list))

print(new_list)

Run Code

Output: [2, 10, 8, 12, 16, 22, 6, 24]

Recursive function in Python

What is recursion?

•Recursion is the process of defining something in terms of itself.

•A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.

Python Recursive Function

•In Python, we know that a function can call other functions.

•It is even possible for the function to call itself.

•These types of construct are termed as recursive functions.

The following image shows the working of a recursive function called recurse 

Example of a Recursive function

def factorial(x):

“””This is a recursive function to find the factorial of an integer”””

if x == 1:

  return 1

else:

  return (x * factorial(x-1))

num = 3

print(“The factorial of”, num, “is”, factorial(num))

Advantages & Disadvantages of Recursive function

Advantages of Recursion

•Recursive functions make the code look clean and elegant.

•A complex task can be broken down into simpler sub-problems using recursion.

•Sequence generation is easier with recursion than using some nested iteration

Disadvantages of Recursion

•Sometimes the logic behind recursion is hard to follow through.

•Recursive calls are expensive (inefficient) as they take up a lot of memory and time.

•Recursive functions are hard to debug.

Leave a Response