Danh sách
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| list/lɪst/ | Danh sách |
| elements/ˈelɪmənts/ | phần tử |
| ordered collection/ˈɔːdəd kəˈlekʃn/ | tập hợp có thứ tự |
| index/ˈɪndeks/ | chỉ số |
| traverse/trəˈvɜːs/ | duyệt qua |
| general/ˈdʒenərəl/ | Phổ quát |
| append/əˈpend/ | thêm vào cuối |
A list holds many elements
- A list 列表 stores an ordered collection 有序集合 of values, called elements 元素, under one name.
- A single list
coloursmight hold "red", "green", and "blue". - One name replaces many separate variables.
- The order is fixed, so each element has a definite position.
Một danh sách lưu trữ:
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Nhiều giá trị, một tên, theo thứ tự cố định.
Access by index
- You access or change an element using its index 索引 — its position in the list.
- In many languages the first element is at index $0$:
colours[0]is "red". - Read
colours[1]to get "green", or assign tocolours[2]to change it. - Always check where a language starts counting.

A list holds many values in one variable, each found by its index
Truy cập một phần tử, hay thay đổi danh sách?
Đọc colours[1] truy cập vào một phần tử; append (thêm), insert (chèn) và remove (xóa) thay đổi nội dung của danh sách.
Nếu chỉ mục bắt đầu từ 0, colours[0] trong ["red","green","blue"] là:
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Chỉ mục 0 là phần tử đầu tiên, "red".
Duyệt qua từng phần tử của danh sách theo trình tự bằng vòng lặp được gọi là ______ danh sách.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Duyệt xử lý toàn bộ danh sách, bất kể kích thước của nó.
Traverse with a loop
- To process a whole list, use iteration to traverse 遍历 it.
- A loop visits every element in turn — first, second, third, and so on.
- The same loop works no matter how many elements the list holds.
- This is what makes list code so general 通用.
Thao tác append (thêm):
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Append làm dài danh sách thêm một phần tử ở cuối.
Duyệt [70, 85, 90] và cộng từng phần tử vào tổng sẽ cho tổng cuối cùng là bao nhiêu?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
70 + 85 + 90 = 245.
Vòng lặp duyệt giống nhau dù danh sách có 3 phần tử hay 300 phần tử.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Vòng lặp tham chiếu đến danh sách bằng tên, khiến nó mang tính tổng quát.
Change the contents
- Lists support operations that change their contents:
- append 追加 adds a new element to the end;
- insert adds an element at a chosen position; remove takes one out.
Total a list. scores holds [70, 85, 90]. Start total ← 0, then traverse: 70 → total 70; 85 → total 155; 90 → total 245. Final total 245. Append a fourth score and the same loop adds it too — no code change.
A list is an ordered collection of elements under one name, reached by index (often from 0). Traverse it with a loop to process every element, which keeps code general for any size. Lists also change: append, insert, and remove.