Python lambda Functions - Work with anonymous functions

Publicado em: 11 Janeiro 2021
no canal de: 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


Nesta página do site você pode assistir ao vídeo on-line Python lambda Functions - Work with anonymous functions duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário TekMinded - Python Recipes 11 Janeiro 2021, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 147 vezes e gostou 6 espectadores. Boa visualização!