Python: using the Lambda operator


/ Published in: Python
Save to your folder(s)

The lambda operator built in to the Python language provides a method to create anonymous functions. This makes it easier to pass simple functions as parameters or assign them to variable names. The lambda operator uses the following syntax to define the function:
lambda :
The term args refers to a list of arguments that get passed to the function. The term expression can be any legal Python expression. The following code shows an example of using the lambda operator to assign an anonymous function to a variable:


Copy this code and paste it in your HTML
  1. >>>bigger = lambda a, b : a > b
  2. >>>print bigger(1,2)
  3. False
  4. >>>print bigger(2,1)
  5. True

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.