near_me
Linear Algebra
keyboard_arrow_down 54 guides
chevron_leftFunctions
check_circle
Mark as learned thumb_up
4
thumb_down
0
chat_bubble_outline
0
Comment auto_stories Bi-column layout
settings
Lambda functions in Python
schedule Aug 12, 2023
Last updated local_offer
Tags Python
tocTable of Contents
expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
Start your free 7-days trial now!
Python's lambda functions are a concise way to return a value from a single expression. They are often used within other functions (Python supports functional programming) and can even be set as the return value.
The general syntax for a lambda function:
lambda arguments: expression
A very basic lambda function that squares x
adds one and returns the result:
lambda x: x**2 + 1
Examples
Multiple arguments
To square x
and y
and return their sum:
a = lambda x,y: x**2 + y**2a(1,2)
5
Input to other functions
To specify a lambda function as an input to map
function:
As return value
To use lambda function as the return value:
def sqrd_plus_n(n): return lambda x: x**2 + n
a = sqrd_plus_n(3) #We set n as 3 (adding 3)a(2) #2**2 + 3
7
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
4
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!