> For the complete documentation index, see [llms.txt](https://python.dainganxanh.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://python.dainganxanh.com/chuong3/bai-17.-ham-an-danh.md).

# Bài 17. Hàm ẩn danh

Hàm ẩn danh không dùng từ khóa def và tên hàm. Cú pháp hàm ẩn danh như sau:

```python
<tên biến> = lambda <tham số>: <biểu thức / giá trị trả về>
```

Ví dụ

```python
tong3 = lambda a, b, c : a + b + c
x = 5
y = 7
z = 3
print(tong3(x,y,z)
```

Output: `15`

Các hàm lambda có thể có nhiều tham số nhưng chỉ có một biểu thức. Các hàm Lambda có thể được sử dụng ở bất cứ nơi nào trong chương trình.

Ví dụ 2

```python
double = lambda x: x * 2
print(double(5))
```

Chương trình trên tương đương với:

```python
def double(x):
   return x * 2
print(double(5))
```

Hàm Lambda được sử dụng cùng với các hàm có sẵn như filter (), map (), v.v.&#x20;

Khi sử dụng cùng với các hàm có sẵn (build-in), lambda thường được dùng như một tham số của hàm build-in.&#x20;

Ví dụ

```python
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x%2 == 0) , my_list))
print(new_list)
```

Output: `[4, 6, 8, 12]`

```python
# Program to double each item in a list using map()

my_list = [1, 5, 4, 6, 8, 11, 3, 12]

new_list = list(map(lambda x: x * 2 , my_list))

print(new_list)
```

Output: `[2, 10, 8, 12, 16, 22, 6, 24]`

Trong ví dụ trên, lambda là tham số thứ nhất của hàm filter(), map().


---

# 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, and the optional `goal` query parameter:

```
GET https://python.dainganxanh.com/chuong3/bai-17.-ham-an-danh.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
