Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- SQL
- iterates
- PANDAS
- Default X points
- error
- self parameter
- For loops
- matplotlib.pyplot
- start exercise
- PROJECT
- machine learning
- Text mining
- line color
- AS
- Github
- MySQL
- data distribution
- Text Analytics
- matplotlib
- break
- pie charts
- continue
- line width
- train/test
- Python
- Else
- variables
- multiple lines
- polynomial regression
- __init__
Archives
- Today
- Total
Data Science Explorer
[Basic Grammar] Python Lambda 본문
반응형
A lambda function is small anonymous function.
- Syntax
lambda arguments : expression
Example
Add 10 to argument a, and return the result.
x = lambda a : a + 10
print (x(8))
Multiply argument a with argument b and return the result.
x = lambda a,b : a * b
print (x(8, 1))
Example
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))
Exercises
Write a Python program that defines a lambda function multiply_by which takes a number n as an argument and returns a new lambda function. The new lambda function should take another number x as an argument and return the result of multiplying n by x. Then, use this lambda function to calculate the product of 5 and 7.
def multiply_by(n):
return lambda a : a * n
triple = multiply_by (3)
result = triple (5)
print ("Result:", result)
Result: 15
'Python' 카테고리의 다른 글
[Basic Grammar] Python Strings (1) | 2023.10.22 |
---|---|
[Basic Grammar] Python Arrays (0) | 2023.10.21 |
[Basic Grammar] Python Functions (0) | 2023.10.21 |
[Basic Grammar] Python For Loops (1) | 2023.10.21 |
[Basic Grammar] Python While Loops (0) | 2023.10.20 |