이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
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.
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.
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.

English
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.
English
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.
English
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,←==.
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.
English
Now you try
- Build, grow, or traverse a list in each task.
- Press Check answer to test your code.
탐색하기
A list of items, indexed
A list stores many values in order, each at an 색인 from 0.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Start from the list colors = ["red", "green"]. Use .append(...) to add "blue" to the end, so the list becomes ["red", "green", "blue"].
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
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.)
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]. Store the 첫 번째 day in first and the 마지막 day in last using indexing.
출력을 보려면 '실행'을 클릭하세요.