3-1-4-11. Lambda Expressions

quick functions that aren’t really needed later in your code.

Later, we’ll learn about higher-order functions.

Are functions that take in other functions as arguments,

where lambda expressions become especially useful.

Let’s compare the structure of a function and a lambda expression.

Here, is a simple function that doubles a number.

It takes in a number x and returns x multiplied by two,

calling double three, would return six.

Here is the equivalent in a lambda expression.

The lambda keyword is used to indicate that this is a lambda expression.

Following lambda or one or more arguments for the anonymous function and then a colon.

These are equivalent, similar to the way argument names in a function are arbitrary.

Last is an expression that is evaluated and returned in this function.

This is a lot like the expression you might see as a return statement in a function.

With this structure, lambda expressions aren’t ideal for complex functions,

but can be very useful for short symbol functions.

If you want to specify multiple arguments in a lambda function,

you can include them before the colon, separated by commas.

Here’s a lambda function that multiplies two numbers together.

In the following quizzes,

you’ll get some practice using lambda functions and see how useful they can be.

Python에서는 람다 식을 사용하여 익명 함수를 만들 수 있습니다.

즉, 이름이 없는 함수입니다.

그들은 생성에 매우 도움이됩니다.

나중에 코드에서 실제로 필요하지 않은 빠른 기능.

나중에 우리는 고차 함수에 대해 배울 것입니다.

다른 함수를 인수로 취하는 함수입니까?

여기서 람다 표현식이 특히 유용합니다.

함수의 구조와 람다 식을 비교해 보겠습니다.

여기에 숫자를 두 배로 만드는 간단한 함수가 있습니다.

숫자 x를 받아서 x에 2를 곱한 값을 반환합니다.

double 3을 호출하면 6이 반환됩니다.

다음은 람다 식에 해당하는 내용입니다.

람다 키워드는 이것이 람다 식임을 나타내는 데 사용됩니다.

익명 함수에 대한 람다 또는 하나 이상의 인수 다음에 콜론이 옵니다.

이는 함수의 인수 이름이 임의적인 것과 유사합니다.

마지막은 이 함수에서 평가되고 반환되는 표현식입니다.

이것은 함수에서 return 문으로 볼 수 있는 표현식과 매우 유사합니다.

이 구조를 사용하면 람다 식은 복잡한 함수에 이상적이지 않습니다.

그러나 짧은 기호 기능에는 매우 유용할 수 있습니다.

람다 함수에 여러 인수를 지정하려면

콜론 앞에 쉼표로 구분하여 포함할 수 있습니다.

다음은 두 숫자를 곱하는 람다 함수입니다.

다음 퀴즈에서는

람다 함수를 사용하는 연습을 하고 얼마나 유용한지 알게 될 것입니다.

Lambda Expressions

You can use lambda expressions to create anonymous functions. That is, functions that don’t have a name. They are helpful for creating quick functions that aren’t needed later in your code. This can be especially useful for higher order functions, or functions that take in other functions as arguments.

With a lambda expression, this function:

def multiply(x, y):
    return x * y

can be reduced to:

multiply = lambda x, y: x * y

Both of these functions are used in the same way. In either case, we can call multiply like this:

multiply(4, 7)

This returns 28.

Components of a Lambda Function

  1. The lambda keyword is used to indicate that this is a lambda expression.
  2. Following lambda are one or more arguments for the anonymous function separated by commas, followed by a colon :. Similar to functions, the way the arguments are named in a lambda expression is arbitrary.
  3. Last is an expression that is evaluated and returned in this function. This is a lot like an expression you might see as a return statement in a function.

With this structure, lambda expressions aren’t ideal for complex functions, but can be very useful for short, simple functions.

%d