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.
日本語
リストは多くの値を保持します
- リストは、複数の値を1つの変数に保持します。
- 項目を角括弧の間に書きます:
[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)は項目の総数を返します。- リストはプログラムの実行に伴って成長します。
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.
日本語
リストの反復処理
for item in nums:は順に各項目を訪問します。- ループ変数は各パスで1つの項目を保持します。
- これはすべての値を処理する簡単な方法です。
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.
日本語
Cambridge擬似コードにおける表現
- リストは試験における1次元配列です。なお、試験の配列は通常インデックス
1から始まりますが、Pythonのリストは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はリストをコピーしません — 両方の名前は同じリストを共有します。本物のコピーを作るにはa.copy()を使用してください。appendは末尾に1つの項目を追加します。
English
Now you try
- Build or loop over a list in each task.
- Press Check answer to test your code.
日本語
あなたも試してみよう
- 各タスクでリストを作成または反復処理します。
- 回答を確認 を押してコードを試してください。
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. · 実行ボタンをクリックして出力を確認してください。