> For the complete documentation index, see [llms.txt](https://yo-sarawut.gitbook.io/tutorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yo-sarawut.gitbook.io/tutorials/tutorials/python/anonymous-lambda-functions/lambda-expression.md).

# Lambda Expression คืออะไร

## Lambda Expression คืออะไร สอนเขียน Anonymous Function ตัวอย่างการใช้งาน Lambda Function ในภาษา Python&#x20;

![](/files/-MDOg-YkOTQGVAGJ3GdD)

Lambda Expression คือ การประกาศฟังก์ชันเล็ก ๆ ที่สร้างขึ้นมาเฉพาะกิจ ในภาษา Python เป็น Anonymous Function คือ ฟังก์ชันนิรนาม ที่ประกาศโดยไม่ได้ตั้งชื่อ การไม่ได้ประกาศฟังก์ชันอย่างเต็มรูปแบบ ความกระชับของโค้ด และการไม่ได้ตั้งชื่อ ทำให้มีข้อดี เหมาะกับใช้ซ้อนในฟังก์ชั่นอื่น

เราสามารถสร้าง Lambda Expression ให้รับ Parameter กี่ตัวก็ได้ แต่ Lambda Function นั้นต้องประกอบด้วย 1 Expression เท่านั้น โดยไม่ต้องระบุ return เพราะ ​Lambda Function จะ return Expression นั้นโดยอัตโนมัติ

### เรามาเริ่มกันเลยดีกว่า

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/gnoparus/bualabs/blob/master/nbs/22b-python-lambda.ipynb)[Check it out on github](https://github.com/gnoparus/bualabs/blob/master/nbs/22b-python-lambda.ipynb) Last updated: 02/07/2020 11:16:12

## ตัวอย่าง Lambda Function <a href="#lambda-function" id="lambda-function"></a>

ตัวอย่าง Lambda Function ที่รับ Parameter มา บวก 1 แล้ว return ผลลัพธ์In \[0]:

```python
x = lambda a : a + 1
x
```

Out\[0]:

```
<function __main__.<lambda>>
```

เรียกฟังก์ชัน ด้วย Parameter 5

In \[0]:

```python
x(5)
```

Out\[0]:

```
6
```

เราสามารถสร้าง Lambda Function ที่รับ Parameter มากกว่า 1 ตัว ดังตัวอย่างด้านล่าง ที่รับ Parameter 2 ตัวIn \[0]:

```python
x2 = lambda a, b : (a * b) + 0.05
```

In \[0]:

```python
x2(2, 3)
```

Out\[0]:

```
6.05
```

หรือ 3 Parameter ดังตัวอย่างIn \[0]:

```python
x3 = lambda a, b, c : (a * b) + c + 700
```

In \[0]:

```python
x3(3, 4, 1000)
```

Out\[0]:

```
1712
```

## ทำไมเราต้องใช้ Lambda Function <a href="#lambda-function" id="lambda-function"></a>

Lambda Function จะมีประโยชน์มาก เมื่อเราใช้เป็น Anonymous Function ภายในฟังก์ชันอื่น ดังตัวอย่าง

สมมติเราต้องการสร้างฟังก์ชัน ที่จะคูณกับค่าที่เรากำหนดIn \[0]:

```python
def multiply_j(j):
    return lambda a : a * j
```

เราจะสร้างฟังก์ชันที่จะคูณ 8In \[0]:

```python
m8 = multiply_j(j=8)
m8
```

Out\[0]:

```
<function __main__.multiply_j.<locals>.<lambda>>
```

In \[0]:

```python
m8(12)
```

Out\[0]:

```
96
```

เราจะสร้าง 3 ฟังก์ชัน ที่คุณกับเลข 3, 5, 7In \[0]:

```python
m3 = multiply_j(3)
m5 = multiply_j(5)
m7 = multiply_j(7)
```

ลองเรียกทั้ง 3 ฟังก์ชัน ด้วย 3In \[0]:

```python
m3(3), m5(3), m7(3)
```

Out\[0]:

```
(9, 15, 21)
```

## ตัวอย่างการใช้งาน Lambda Function <a href="#lambda-function" id="lambda-function"></a>

เราสามารถประกาศ Lambda Function พร้อม ๆ กับ Call ได้เลยIn \[0]:

```python
 (lambda x: x + x)(12) 
```

Out\[0]:

```
24
```

เราสามารถสร้าง Lambda Function ส่งให้กับฟังก์ชันที่ต้องการ Parameter เป็นฟังก์ชัน แทนที่จะต้องประกาศฟังก์ชันแบบเต็มรูปแบบ เช่น ฟังก์ชัน filter ต้องการ Parameter เป็นฟังก์ชัน ที่ return True ให้ x ผ่านการกรองIn \[0]:

```python
s = [85, 44, 3, 22, 1, 32, 24, 49, 11, 26, 20, 0, 10]
result = filter (lambda x: x > 20, s) 
print(list(result))
```

```
[85, 44, 22, 32, 24, 49, 26]
```

## Credit <a href="#credit" id="credit"></a>

* <https://course.fast.ai/videos/?lesson=10>
* <https://www.w3schools.com/python/python_lambda.asp>
* <https://en.wikipedia.org/wiki/Anonymous_function>

#### บทความที่เกี่ยวข้อง:

1. [Callback Function คืออะไร สอนเขียน Callback ฟังก์ชั่น ตัวอย่างการใช้งาน Callback ในภาษา Python – Python ep.6](https://www.bualabs.com/archives/2238/what-is-callback-function-python-ep-6/)
2. [Partial Function คืออะไร สอนเขียน Partial Function ตัวอย่างการใช้งาน functools.partial ในภาษา Python – Python ep.8](https://www.bualabs.com/archives/2281/what-is-partial-function-teach-how-to-write-partial-function-usage-python-functools-partial-python-ep-8/)
3. [args, kwargs คืออะไร สอนเขียน Function ที่ใช้ args, kwargs ตัวอย่างการใช้งาน args, kwargs ในภาษา Python – Python ep.9](https://www.bualabs.com/archives/2289/what-is-args-kwargs-teach-how-to-write-function-args-kwargs-python-ep-9/)
4. [Refactor โค้ด Neural Network สร้าง DataBunch และ Learner ปรับปรุง Training Loop – Neural Network ep.9](https://www.bualabs.com/archives/2318/databunch-learner-refactor-neural-network-training-loop-neural-network-ep-9/)

Reference : <https://www.bualabs.com/archives/2252/what-is-lambda-function-teach-lambda-function-python-ep-7/#more-2252>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yo-sarawut.gitbook.io/tutorials/tutorials/python/anonymous-lambda-functions/lambda-expression.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
