Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Lambda Functions | Recursion and Lambda Functions
Python Functions Tutorial
course content

Kurssisisältö

Python Functions Tutorial

Python Functions Tutorial

1. What is a Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

book
Lambda Functions

Lambda functions are anonymous functions, meaning they don't have a name. They are created using the lambda keyword and are often used to define short functions where you can specify a function on the spot.

The basic syntax of a lambda function is as follows:

python
  • lambda: the keyword indicating the start of a lambda function definition;
  • arguments: the list of arguments the function takes;
  • expression: the expression executed when the function is called. The result of the expression is returned as the function's value.

The key feature of lambda functions is their concise syntax. They are convenient when you need to define a simple function without writing a lot of code.

123
square = lambda x: x**2 result = square(5) print(result)
copy
Tehtävä

Swipe to start coding

There is a list of prices (prices), and a lambda expression needs to be implemented that takes a price as a parameter and deducts 13% tax from it.

  1. Define a lambda expression using the lambda keyword.
  2. The lambda expression should take one parameter (price).
  3. If the price is negative, consider it invalid and return 0 using the max() function directly within the lambda expression.
  4. The lambda should first check the price and then deduct 13% from the valid amount.
  5. Use a list comprehension to apply apply_tax to each element in prices.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 5. Luku 3
toggle bottom row

book
Lambda Functions

Lambda functions are anonymous functions, meaning they don't have a name. They are created using the lambda keyword and are often used to define short functions where you can specify a function on the spot.

The basic syntax of a lambda function is as follows:

python
  • lambda: the keyword indicating the start of a lambda function definition;
  • arguments: the list of arguments the function takes;
  • expression: the expression executed when the function is called. The result of the expression is returned as the function's value.

The key feature of lambda functions is their concise syntax. They are convenient when you need to define a simple function without writing a lot of code.

123
square = lambda x: x**2 result = square(5) print(result)
copy
Tehtävä

Swipe to start coding

There is a list of prices (prices), and a lambda expression needs to be implemented that takes a price as a parameter and deducts 13% tax from it.

  1. Define a lambda expression using the lambda keyword.
  2. The lambda expression should take one parameter (price).
  3. If the price is negative, consider it invalid and return 0 using the max() function directly within the lambda expression.
  4. The lambda should first check the price and then deduct 13% from the valid amount.
  5. Use a list comprehension to apply apply_tax to each element in prices.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 5. Luku 3
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt