Python lambda Functions - Work with anonymous functions

Published: 11 January 2021
on channel: TekMinded - Python Recipes
147
6

What are lambdas or anonymous functions?
Anonymous functions also know as lambdas are functions that behave like regular functions buy are by default not reusable. Lambda functions are small and usually are written in one single line.

Lambdas structure include the following elements:
Keyword: lambda
Variables: any number of named variables (example: x, y, z)
Expression: calculation to be executed by the function.

before we start let's create a regular function
def add_one(a):
return + 1

l# if we pass a 2 as value, it returns 3
add_one(2)
////3

let's do the same using lambda

(lambda a: a + 1)(2)
////3

we can assign lambdas to variables
add_one_lambda = lambda a : a + 1

add_one_lambda(2)
////3

let's see another example
multiply = lambda a, b : a * b

multiply (2,3)
////6

list (map (lambda x:x**2, [2,3,4,5]))
///[4, 9, 16, 25]

In conclusion: Lambdas or anonymous functions are a clever and pythonic way to create functions where when reusability is less important that readability.

#python #data #lambda #functions


On this page of the site you can watch the video online Python lambda Functions - Work with anonymous functions with a duration of hours minute second in good quality, which was uploaded by the user TekMinded - Python Recipes 11 January 2021, share the link with friends and acquaintances, this video has already been watched 147 times on youtube and it was liked by 6 viewers. Enjoy your viewing!