Lists and data abstraction
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
English
Why a list?
- A program often needs many values at once, like a class of scores.
- One variable per value would be messy.
- A list holds many values under one name — a useful data abstraction.
Tiếng Việt
Tại sao dùng danh sách?
- Một chương trình thường cần nhiều giá trị cùng lúc, ví dụ như điểm của cả lớp.
- Một biến cho mỗi giá trị sẽ rất lộn xộn.
- Một danh sách chứa nhiều giá trị dưới một tên duy nhất — đây là một trừu tượng dữ liệu hữu ích.
English
What is a data abstraction?
- An abstraction hides detail so you can think about the whole, not the parts.
- A list lets you treat 1 value or 1000 values the same way.
- You write the same code no matter how long the list is.
Tiếng Việt
Trừu tượng dữ liệu là gì?
- Một trừu tượng ẩn đi chi tiết để bạn có thể nghĩ đến toàn bộ, thay vì từng phần nhỏ.
- Danh sách cho phép bạn xử lý 1 giá trị hay 1000 giá trị theo cùng một cách.
- Bạn viết cùng một đoạn mã bất kể danh sách dài bao nhiêu.
English
Make a list and read it
- Write items inside square brackets:
[10, 20, 30]. - Get one item by its index (its position). Python starts at
0. scores[0]is the first item;scores[-1]is the last item.
Tiếng Việt
Tạo và đọc danh sách
- Viết các mục bên trong dấu ngoặc vuông:
[10, 20, 30]. - Lấy một mục bằng chỉ số (vị trí của nó). Python bắt đầu từ
0. scores[0]là mục đầu tiên;scores[-1]là mục cuối cùng.
scores = [10, 20, 30]
print(scores[0])
print(scores[-1])
print(len(scores))
English
Grow a list with append
scores.append(x)addsxto the end of the list.- The list can grow while the program runs.
len(scores)tells you how many items it now has.
Tiếng Việt
Mở rộng danh sách với append
scores.append(x)thêmxvào cuối danh sách.- Danh sách có thể phát triển khi chương trình đang chạy.
len(scores)cho biết hiện tại có bao nhiêu mục.
scores = [10, 20]
scores.append(30)
print(scores)
print(len(scores))
English
Traverse (visit every item)
- To traverse a list means to go through every item, one by one.
for s in scores:gives you one item each pass — this is a for-each loop.- It is the clean way to process the whole list.
Tiếng Việt
Duyệt (thăm tất cả các mục)
- Để duyệt một danh sách nghĩa là đi qua từng mục, lần lượt từng cái.
for s in scores:cung cấp một mục mỗi lần lặp — đây là vòng lặp for-each.- Đây là cách sạch sẽ để xử lý toàn bộ danh sách.
scores = [10, 20, 30]
total = 0
for s in scores:
total = total + s
print(total)
English
In AP CSP pseudocode
- A list is the exam's list too — but CB lists are 1-indexed.
- So
aList[1]is the first element in CB, while Python's first isscores[0]. FOR EACH x IN listmatches Python'sfor x in list:.DISPLAY= print,←==.
Tiếng Việt
Trong伪代码 của AP CSP
- Một danh sách cũng xuất hiện trong đề thi — nhưng CB sử dụng chỉ số bắt đầu từ 1.
- Vì vậy
aList[1]là phần tử đầu tiên trong CB, trong khi phần tử đầu tiên của Python làscores[0]. FOR EACH x IN listtương ứng vớifor x in list:của Python.DISPLAY= in,←==.
aList ← [10, 20, 30]
DISPLAY aList[1] ## the FIRST item (1-indexed!)
total ← 0
FOR EACH x IN aList
{
total ← total + x
}
DISPLAY total
English
Common mistakes
- Index a list from
0; the last item isa[-1]. - A list groups related values under one name.
Tiếng Việt
Lỗi thường gặp
- Chỉ số danh sách bắt đầu từ
0; mục cuối cùng làa[-1]. - Danh sách nhóm các giá trị liên quan dưới một tên chung.
English
Now you try
- Build, grow, or traverse a list in each task.
- Press Check answer to test your code.
Tiếng Việt
Bây giờ bạn thử
- Xây dựng, mở rộng hoặc duyệt danh sách trong mỗi bài tập.
- Nhấn Check answer (Kiểm tra câu trả lời) để thử mã của bạn.
Explore · Khám phá
A list of items, indexed
A list stores many values in order, each at an index from 0.
Start from the list colors = ["red", "green"]. Use .append(...) to add "blue" to the end, so the list becomes ["red", "green", "blue"].
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
nums is [4, 8, 15, 16, 23]. Traverse it with a for loop to add up every value, then print the total. (It should print 66.)
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]. Store the first day in first and the last day in last using indexing.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.