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(56))

Example

Summarize argument ab, and c and return the result:

x = lambda a, b, c : a + b + c
print(x(562))



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 Arrays

  Python  Arrays Note:  Python does not have built-in support for Arrays, but  Python Lists  can be used instead. Arrays Note:  This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the  NumPy library . Arrays are used to store multiple values in one single variable: Example Create an array containing car names: cars = [ "Ford" ,  "Volvo" ,  "BMW" ] What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: car1 =  "Ford" car2 =  "Volvo" car3 =  "BMW" However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300? The solution is an array! An array can hold many values under a single name, and you can access the values by referring to an index nu...

Python Strings

  Python  Strings  Strings Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello'  is the same as  "hello" . You can display a string literal with the  print()  function: Example print ( "Hello" ) print ( 'Hello' ) Assign String to a Variable Assigning a string to a variable is done with the variable name followed by an equal sign and the string: Example a =  "Hello" print (a) Multiline Strings You can assign a multiline string to a variable by using three quotes: Example You can use three double quotes: a =  "" "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." "" print (a) Or three single quotes: Example a =  '' 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' '' print (a) Note:  in the res...