14

LAMBDA FUNCTIONS

by

The Education Machine

[support@theeducationmachine.com]

Lambda functions are anonymous functions which does not specify the function name when we print them.

Functions that we discussed in last sessions are defined using 'def' keyword however Lambda functions are defined using lambda keyword.

Lambda functions are not meant to write complex programs which can be done using normal functions.

Lambda functions can take any number of arguments, but can only have one expression.



  def bigger(num1,num2):
      if num1 > num2:
          return num1
      return num2
  
  print(bigger)

Output:
<function bigger at 0x0000024658373A60>

We created above function 'bigger' & printed it but not performed function call. We can notice that function name is returned as output which is stored at some address. Do not compare this value with the memory address returned using id( ) function. id() of bigger is different.

  print(id(bigger), type(bigger))

Output:
2395082532616 <class 'function'>

Function to print number * 10

  def multiply(num):
      print(num * 10)
  
  multiply(2)
  print(multiply, type(multiply))

Using Lambda Function:
Lambda functions are defined as- Variable name = name of function i.e. lambda, then arguments, colon and expression. As shown below:

  mult = lambda num: num * 20
  print(mult(2))
  print(mult, type(mult))

Output:
20
<function multiply at 0x0000022DA60B2CA8> <class 'function'>
40
<function <lambda> at 0x0000022DA60B2F78> <class 'function'>

In above examples, both functions belong to class 'function' however it can be seen that when we printed 'multiply' function, it has printed function name i.e. multiply.
But when we printed 'mult' function, it has not printed the function name. Hence, Lambda functions can be called as anonymous functions.

Let us try using 3 arguments in lambda function.
Notice that there are multiple arguments but only one expression.

  average_salary = lambda jan, feb, mar : (jan + feb + mar) / 3
  print(average_salary(100,200,300))

Above program can be written as:

  average_salary = lambda jan, feb, mar : (jan + feb + mar) / len((jan,feb,mar))
  print(average_salary(100,200,300))

Output:
200.0
200.0

Create a lambda function to reverse a string.

  reversed_string = lambda string : string[::-1]
  print(reversed_string('Python'))

To print value within the lambda function.

  reversed_string = lambda string : print(string[::-1])
  reversed_string('Python')

Output:
nohtyP
nohtyP

Can we use if-else condition within a lambda function?
YES!!

  odd_even = lambda num : print("even") if num%2 == 0 else print("odd")
  odd_even(9)
  odd_even(4)

Output:
odd
even

How to use if-elif-else condition in Lambda function.

  age_check = lambda age : print("Kid") if age <= 10 print("Adolescence") if age in range(11,20) else (print("Adult") if age in range(20,51) else print("Older"))
  age_check(78)

reserved_keywords

Direct elif does not work but below method is an alternative to it.

  age_check = lambda age : print("Kid") if age <= 10 else (print("Adult") if age in range(11,51) else print("Older"))
  age_check(78)

Output:
Older

Expanding it more:

  age_check = lambda age : print("Kid") if age <= 10 else (print("Adult") if age in range(11,51) else (print("Older") if age in range(60,91) else print("You are FIT")))
  
  age_check(8)
  age_check(18)
  age_check(38)
  age_check(68)
  age_check(98)

Output:
Kid
Adult
Adult
Older
You are FIT

Return a lambda function from a regular function.

  def mult(num):
      return lambda num1 : num1 * num
  
  print(mult)
  multiply = mult(9)
  print(multiply)
  print(multiply(5))

Output:
<function mult at 0x0000022DA60D50D8>
<function mult.<locals>.<lambda> at 0x0000022DA60EA3A8>
45

In above code, mult is a regular function however lambda function-> multiply is created as an instance of mult. Therefore, in output "function mult.locals.lambda" is written which means this lambda function will always point to mult function

Function that checks if given string is palindrome or not using lambda.

  palindrome = lambda string: print("Yes! Palindrome") if string[::-1] == string else print("Its not a palindrome")
  
  palindrome("NITIN")
  palindrome("KASHISH")

Output:
Yes! Palindrome
Its not a palindrome