Lists (1-D arrays)
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
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.
한국어
목록은 여러 값을 담습니다
- 리스트(list) 는 하나의 변수에 여러 값을 유지합니다.
- 대괄호 안에 항목들을 나열하세요:
[10, 20, 30]. - 항목들은 숫자, 문자열, 혹은 그 조합일 수 있습니다.
English
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.
한국어
항목 가져오기와 변경하기
- 인덱스를 사용하여 문자열처럼 접근하세요:
nums[0]가 첫 번째 항목입니다. nums[-1]가 마지막 항목입니다.- 또한 항목을 변경할 수도 있습니다:
nums[0] = 99.
fruits = ["apple", "pear", "plum"]
print(fruits[0])
print(fruits[-1])
English
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.
한국어
항목 추가 및 세기
nums.append(x)는x를 리스트의 끝에 추가합니다.len(nums)은 항목의 총 개수를 알려줍니다.- 프로그램이 실행됨에 따라 목록(list)은 커질 수 있습니다.
fruits = ["apple", "pear"]
fruits.append("plum")
print(len(fruits))
print(fruits)
English
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.
한국어
목록을 순환(loop)하기
for item in nums:는 순차적으로 각 항목을 방문합니다.- 루프 변수는 매 반복 시 하나의 항목을 가집니다.
- 모든 값을 처리하는 가장 쉬운 방법입니다.
scores = [10, 20, 30]
total = 0
for s in scores:
total = total + s
print(total)
English
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.
한국어
캐미지아 가위코드에서
- 목록(list)은 시험의 1차원 배열입니다. 참고: 시험용 배열은 보통 인덱스
1에서 시작하지만, Python 목록(list)은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
English
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.
한국어
흔한 실수
- 첫 번째 항목은
a[0]이며, 마지막은a[-1]입니다;a[len(a)]는 범위 범위를 벗어납니다. b = a는 목록(list)을 복사하지 않습니다 — 두 이름이 같은 것을 공유합니다. 실제 복사를 만들려면a.copy()를 사용하십시오.append는 한 개의 항목을 끝에 추가합니다.
English
Now you try
- Build or loop over a list in each task.
- Press Check answer to test your code.
한국어
이제 직접 해보기
- 각 작업에서 목록(list)을 생성하거나 순환(loop)하십시오.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
Explore · 탐색하기
A list changes in place
A list is ordered and indexed; append/change/pop all modify it.
Create a list colors holding "red", "green", "blue". Then append "yellow" so the list has four items.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Loop over the list nums and add up its values. Store the total in total (the answer is 66).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
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).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Build a new list backwards with the items of nums in reverse order. Use a loop, and do not change nums itself.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.