Skip to main content

Python Lambda Function

 

Python Lambda


A lambda function is a small anonymous function.

A lambda function can take any number of arguments, but can only have one expression.


Syntax

lambda arguments : expression

The expression is executed and the result is returned:

Example

Add 10 to argument a, and return the result:

x = lambda a : a + 10
print(x(5))

Lambda functions can take any number of arguments:

Example

Multiply argument a with argument b and return the result:

x = lambda a, b : a * b
print(x(5, 6))

Example

Summarize argument a, b, and c and return the result:

x = lambda a, b, c : a + b + c
print(x(5, 6, 2))



Why Use Lambda Functions?

The power of lambda is better shown when you use them as an anonymous function inside another function.

Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number:

def myfunc(n):
  return lambda a : a * n

Use that function definition to make a function that always doubles the number you send in:

Example

def myfunc(n):
  return lambda a : a * n

mydoubler = myfunc(2)

print(mydoubler(11))

Or, use the same function definition to make a function that always triples the number you send in:

Example

def myfunc(n):
  return lambda a : a * n

mytripler = myfunc(3)

print(mytripler(11))

Or, use the same function definition to make both functions, in the same program:

Example

def myfunc(n):
  return lambda a : a * n

mydoubler = myfunc(2)
mytripler = myfunc(3)

print(mydoubler(11))
print(mytripler(11))

Comments

Popular posts from this blog

Python Tuples

  Python  Tuples mytuple = ( "apple" ,  "banana" ,  "cherry" ) Tuple Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and  unchangeable . Tuples are written with round brackets. Example Create a Tuple: thistuple = ( "apple" ,  "banana" ,  "cherry" ) print (thistuple) Tuple Items Tuple items are ordered, unchangeable, and allow duplicate values. Tuple items are indexed, the first item has index  [0] , the second item has index  [1]  etc. Ordered When we say that tuples are ordered, it means that the items have a defined order, and that order will not change. Unchangeable Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created. Allow Duplicates Since tuples...

Python Math Module

  Python  Math Python has a set of built-in math functions, including an extensive math module, that allows you to perform mathematical tasks on numbers. Built-in Math Functions The  min()  and  max()  functions can be used to find the lowest or highest value in an iterable: Example x =  min ( 5 ,  10 ,  25 ) y =  max ( 5 ,  10 ,  25 ) print (x) print (y) The  abs()  function returns the absolute (positive) value of the specified number: Example x =  abs (- 7.25 ) print (x) The  pow( x ,  y )  function returns the value of x to the power of y (x y ). Example Return the value of 4 to the power of 3 (same as 4 * 4 * 4): x =  pow ( 4 ,  3 ) print (x) The Math Module Python has also a built-in module called  math , which extends the list of mathematical functions. To use it, you must import the  math  module: import  math When you have imported the  math  module,...

Python Loops

  While Loops Python Loops Python has two primitive loop commands: while  loops for  loops The while Loop With the  while  loop we can execute a set of statements as long as a condition is true. Example Print i as long as i is less than 6: i =  1 while  i <  6 :    print (i)   i +=  1 Note:  remember to increment i, or else the loop will continue forever. The  while  loop requires relevant variables to be ready, in this example we need to define an indexing variable,  i , which we set to 1. The break Statement With the  break  statement we can stop the loop even if the while condition is true: Example Exit the loop when i is 3: i =  1 while  i <  6 :    print (i)    if  i ==  3 :      break   i +=  1 The continue Statement With the  continue  statement we can stop the current iteration, and continue...