Lists and data abstraction · 리스트와 추상화
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.
왜 리스트일까요?
- 프로그램은 클래스의 점수처럼 여러 값을 한 번에 필요로 하는 경우가 많습니다.
- 값마다 변수를 하나씩 쓰면 지저분해집니다.
- 리스트는 하나의 이름 아래에 여러 값을 저장하여 유용한 데이터 추상화가 됩니다.
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.
데이터 추상화가 무엇인가요?
- 추상화는 디테일을 숨겨 전체에 집중하고 세부 사항에 집중하지 않게 해줍니다.
- 리스트는 1개의 값이나 1000개의 값을 동일한 방식으로 다룰 수 있게 합니다.
- 리스트의 길이가 어떻게 되든 동일한 코드를 작성할 수 있습니다.
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.
리스트 생성 및 읽기
- 대괄호 안에 항목을 나열합니다:
[10, 20, 30]. - 인덱스(위치)로 특정 항목을 얻습니다. Python은
0부터 시작합니다. scores[0]는 첫 번째 항목이며,scores[-1]는 마지막 항목입니다.
scores = [10, 20, 30]
print(scores[0])
print(scores[-1])
print(len(scores))
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.
append로 리스트 확장
scores.append(x)는x를 리스트의 끝에 추가합니다.- 프로그램 실행 중에도 리스트는 커질 수 있습니다.
len(scores)는 현재 총 항목 수를 알려줍니다.
scores = [10, 20]
scores.append(30)
print(scores)
print(len(scores))
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.
순환(모든 항목 방문)
- 리스트를 순환한다는 것은 한 개씩 모든 항목을 지나가는 것입니다.
for s in scores:는 매 반복 시 하나의 항목을 제공합니다 — 이는 for-each 루프입니다.- 전체 리스트를 처리하는 가장 깔끔한 방법입니다.
scores = [10, 20, 30]
total = 0
for s in scores:
total = total + s
print(total)
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,←==.
AP CSP 가위수식에서
- 리스트는 시험에서도 자주 등장하지만, CB의 리스트는 1-indexed입니다.
- 따라서 CB에서
aList[1]는 첫 번째 요소이며, Python에서 첫 번째는scores[0]입니다. FOR EACH x IN list는 Python의for 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
Common mistakes
- Index a list from
0; the last item isa[-1]. - A list groups related values under one name.
흔한 실수
- 리스트를
0부터 인덱싱하며, 마지막 항목은a[-1]입니다. - 리스트는 하나의 이름 아래에 관련된 값들을 묶어둡니다.
Now you try
- Build, grow, or traverse a list in each task.
- Press Check answer to test your code.
이제 직접 해보기
- 각 작업에서 리스트를 생성, 확장하거나 순환하십시오.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
A list of items, indexed · 인덱스가 있는 항목의 목록
A list stores many values in order, each at an index from 0. · 리스트는 여러 값을 순서대로 저장하며, 각 값은 0부터 시작하는 인덱스에 위치합니다.
Start from the list colors = ["red", "green"]. Use .append(...) to add "blue" to the end, so the list becomes ["red", "green", "blue"]. · 리스트 colors = ["red", "green"]에서 시작합니다. .append(...)를 사용하여 "blue"를 끝부분에 추가하면, 리스트가 ["red", "green", "blue"]가 됩니다.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
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.) · nums은(는) [4, 8, 15, 16, 23]입니다. for 루프를 사용하여 모든 값을 더한 후, print을 출력합니다. (출력값은 66여야 합니다.)
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]. Store the first day in first and the last day in last using indexing. · days = ["Mon", "Tue", "Wed", "Thu", "Fri"]입니다. 인덱싱을 사용하여 첫째 날을 first에, 마지막 날을 last에 저장합니다.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.