Lists (1-D arrays) · Danh sách (mảng 1-D)
A list holds many values
- A list keeps many values in one variable.
- Write the items inside square brackets:
[10, 20, 30]. - The items can be numbers, strings, or a mix.
Một danh sách chứa nhiều giá trị
- Một danh sách (list) lưu trữ nhiều giá trị trong một biến.
- Viết các phần tử bên trong dấu ngoặc vuông:
[10, 20, 30]. - Các phần tử có thể là số, chuỗi ký tự, hoặc sự kết hợp của cả hai.
Get and change items
- Use an index, just like a string:
nums[0]is the first item. nums[-1]is the last item.- You can also change an item:
nums[0] = 99.
Lấy và thay đổi các phần tử
- Sử dụng chỉ số (index), giống như chuỗi ký tự:
nums[0]là phần tử đầu tiên. nums[-1]là phần tử cuối cùng.- Bạn cũng có thể thay đổi một phần tử:
nums[0] = 99.
fruits = ["apple", "pear", "plum"]
print(fruits[0])
print(fruits[-1])
Add items and count them
nums.append(x)addsxto the end of the list.len(nums)tells you how many items there are.- A list can grow as your program runs.
Thêm phần tử và đếm chúng
nums.append(x)thêmxvào cuối danh sách.len(nums)cho biết có bao nhiêu mục.- Danh sách có thể mở rộng khi chương trình chạy.
fruits = ["apple", "pear"]
fruits.append("plum")
print(len(fruits))
print(fruits)
Loop over a list
for item in nums:visits each item in turn.- The loop variable holds one item per pass.
- This is the easy way to process every value.
Lặp qua một danh sách
for item in nums:thăm từng mục theo thứ tự.- Biến vòng lặp giữ một mục mỗi lần lặp.
- Đây là cách dễ dàng để xử lý mọi giá trị.
scores = [10, 20, 30]
total = 0
for s in scores:
total = total + s
print(total)
In Cambridge pseudocode
- A list is the exam's 1-D array. Note: exam arrays often start at index
1, but Python lists start at0.
Trong pseudocode Cambridge
- Trong đề thi, danh sách chính là mảng 1 chiều. Lưu ý: mảng trong đề thi thường bắt đầu từ chỉ số
1, nhưng danh sách Python bắt đầu từ0.
DECLARE Scores : ARRAY[1:3] OF INTEGER
Scores[1] ← 10
Scores[2] ← 20
Scores[3] ← 30
FOR I ← 1 TO 3
OUTPUT Scores[I]
NEXT I
Common mistakes
- The first item is
a[0]and the last isa[-1];a[len(a)]is out of range. b = adoes not copy the list — both names share it. Usea.copy()to make a real copy.appendadds one item to the end.
Lỗi thường gặp
- Phần tử đầu tiên là
a[0]và phần tử cuối làa[-1];a[len(a)]nằm ngoài phạm vi hợp lệ. b = akhông tạo bản sao của danh sách — cả hai tên đều trỏ vào cùng một đối tượng. Hãy dùnga.copy()để tạo một bản sao thực sự.appendthêm một phần tử vào cuối danh sách.
Now you try
- Build or loop over a list in each task.
- Press Check answer to test your code.
Bây giờ bạn thử
- Xây dựng hoặc lặp qua 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.
A list changes in place · Danh sách thay đổi ngay tại chỗ
A list is ordered and indexed; append/change/pop all modify it. · Danh sách được sắp xếp và có chỉ mục; append/change/pop đều sửa đổi nó.
Create a list colors holding "red", "green", "blue". Then append "yellow" so the list has four items. · Tạo danh sách colors chứa "red", "green", "blue". Sau đó append "yellow" để danh sách có bốn phần tử.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Loop over the list nums and add up its values. Store the total in total (the answer is 66). · Lặp qua danh sách nums và cộng tổng các giá trị của nó. Lưu tổng vào total (đáp án là 66).
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Find the largest value in nums with a loop. Start largest at the first item, then loop through nums and keep the bigger value. Store the answer in largest (it is 23). · Tìm giá trị lớn nhất trong nums bằng vòng lặp. Khởi tạo largest ở phần tử đầu tiên, sau đó lặp qua nums và giữ lại giá trị lớn hơn. Lưu đáp án vào largest (nó là 23).
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Build a new list backwards with the items of nums in reverse order. Use a loop, and do not change nums itself. · Xây dựng một danh sách mới backwards với các phần tử của nums theo thứ tự đảo ngược. Sử dụng vòng lặp, và không thay đổi nums chính nó.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.