Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lambda Function | Function as an Argument
Intermediate Python Techniques
course content

Course Content

Intermediate Python Techniques

Intermediate Python Techniques

1. Packing and Unpacking
2. Arguments in Function
3. Function as an Argument
4. Variable Scope
5. Decorators

bookLambda Function

In Python, lambda functions are anonymous functions defined using the lambda keyword. They are often used for short, single-operation functions without a name, and can be passed as arguments to other functions, just like regular functions.

Here's an example demonstrating how to pass a lambda function as an argument to another function:

12345678
# Define a function that takes a function and a value as arguments def apply_function(func, value): return func(value) # Call the function with a lambda function as the first argument result = apply_function(lambda x: x * x, 5) print(result)
copy
  • apply_function is a function that accepts another function (func) and a value (value), and then applies func to value.
  • A lambda function lambda x: x * x is defined inline and passed as an argument to apply_function. This lambda function squares its input.
  • The apply_function is called with the lambda function and the value 5, resulting in the lambda function squaring 5, which yields 25.

Task

Suppose you have a list of numbers, and you want to apply different operations to the same list, like adding a constant value to each element or multiplying each element by a constant.

  1. apply_to_list is our custom function that applies a given function (func) to each element in numbers.
  2. We call apply_to_list twice with different lambda functions.
  3. The first lambda function (lambda x: x + 10) adds 10 to each element.
  4. The second lambda function (lambda x: x * 2) multiplies each element by 2.

Actually, we are creating our own version of the map function.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

bookLambda Function

In Python, lambda functions are anonymous functions defined using the lambda keyword. They are often used for short, single-operation functions without a name, and can be passed as arguments to other functions, just like regular functions.

Here's an example demonstrating how to pass a lambda function as an argument to another function:

12345678
# Define a function that takes a function and a value as arguments def apply_function(func, value): return func(value) # Call the function with a lambda function as the first argument result = apply_function(lambda x: x * x, 5) print(result)
copy
  • apply_function is a function that accepts another function (func) and a value (value), and then applies func to value.
  • A lambda function lambda x: x * x is defined inline and passed as an argument to apply_function. This lambda function squares its input.
  • The apply_function is called with the lambda function and the value 5, resulting in the lambda function squaring 5, which yields 25.

Task

Suppose you have a list of numbers, and you want to apply different operations to the same list, like adding a constant value to each element or multiplying each element by a constant.

  1. apply_to_list is our custom function that applies a given function (func) to each element in numbers.
  2. We call apply_to_list twice with different lambda functions.
  3. The first lambda function (lambda x: x + 10) adds 10 to each element.
  4. The second lambda function (lambda x: x * 2) multiplies each element by 2.

Actually, we are creating our own version of the map function.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

bookLambda Function

In Python, lambda functions are anonymous functions defined using the lambda keyword. They are often used for short, single-operation functions without a name, and can be passed as arguments to other functions, just like regular functions.

Here's an example demonstrating how to pass a lambda function as an argument to another function:

12345678
# Define a function that takes a function and a value as arguments def apply_function(func, value): return func(value) # Call the function with a lambda function as the first argument result = apply_function(lambda x: x * x, 5) print(result)
copy
  • apply_function is a function that accepts another function (func) and a value (value), and then applies func to value.
  • A lambda function lambda x: x * x is defined inline and passed as an argument to apply_function. This lambda function squares its input.
  • The apply_function is called with the lambda function and the value 5, resulting in the lambda function squaring 5, which yields 25.

Task

Suppose you have a list of numbers, and you want to apply different operations to the same list, like adding a constant value to each element or multiplying each element by a constant.

  1. apply_to_list is our custom function that applies a given function (func) to each element in numbers.
  2. We call apply_to_list twice with different lambda functions.
  3. The first lambda function (lambda x: x + 10) adds 10 to each element.
  4. The second lambda function (lambda x: x * 2) multiplies each element by 2.

Actually, we are creating our own version of the map function.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

In Python, lambda functions are anonymous functions defined using the lambda keyword. They are often used for short, single-operation functions without a name, and can be passed as arguments to other functions, just like regular functions.

Here's an example demonstrating how to pass a lambda function as an argument to another function:

12345678
# Define a function that takes a function and a value as arguments def apply_function(func, value): return func(value) # Call the function with a lambda function as the first argument result = apply_function(lambda x: x * x, 5) print(result)
copy
  • apply_function is a function that accepts another function (func) and a value (value), and then applies func to value.
  • A lambda function lambda x: x * x is defined inline and passed as an argument to apply_function. This lambda function squares its input.
  • The apply_function is called with the lambda function and the value 5, resulting in the lambda function squaring 5, which yields 25.

Task

Suppose you have a list of numbers, and you want to apply different operations to the same list, like adding a constant value to each element or multiplying each element by a constant.

  1. apply_to_list is our custom function that applies a given function (func) to each element in numbers.
  2. We call apply_to_list twice with different lambda functions.
  3. The first lambda function (lambda x: x + 10) adds 10 to each element.
  4. The second lambda function (lambda x: x * 2) multiplies each element by 2.

Actually, we are creating our own version of the map function.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt