1-D arrays (lists)
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 (the exam calls it an array) stores many values under one name.
- Write the items in square brackets, separated by commas.
- An array normally stores items of the same type.
日本語
リストは多くの値を保持します
- リスト(試験では配列と呼ぶ)は、一つの名称の下に多くの値を格納します。
- 要素を角括弧で囲み、コンマで区切って書きます。
- 配列は通常、同じ型の要素を格納します。
scores = [40, 55, 72, 90]
print(scores)
English
Reach an item by its index
- Each item has a position number called its index.
- Python counts from 0, so the first item is
scores[0].
日本語
インデックスでアイテムにアクセス
- 各アイテムには位置番号であるインデックスがあります。
- Pythonは0から数えるため、最初のアイテムは
scores[0]です。
scores = [40, 55, 72, 90]
print(scores[0]) # 40
print(scores[2]) # 72
Explore · 探索
Index and change a list
Items are numbered from 0; scores[0] is the first item.
English
Change an item
- Assign to an index to replace that one item.
日本語
アイテムの変更
- インデックスに割り当てると、そのアイテムが置き換わります。
scores = [40, 55, 72, 90]
scores[1] = 99
print(scores) # [40, 99, 72, 90]
English
Length and looping
len(scores)gives how many items there are.- A
forloop visits each item in turn.
日本語
長さとループ
len(scores)はアイテムの総数を返します。forループは順次各アイテムを訪問します。
scores = [40, 55, 72, 90]
print(len(scores)) # 4
for s in scores:
print(s)
English
In the exam: arrays start at 1
- Pseudocode declares an array with a size, and usually counts from 1.
日本語
試験対策: 配列は1から始まる
- 疑似コードではサイズを指定して配列を宣言し、通常1から数えます。
DECLARE Scores : ARRAY[1:4] OF INTEGER
Scores[1] ← 40
OUTPUT Scores[1]
English
Common mistakes
- Index from
0; the last item is at positionlength - 1. - Going past the end gives an index error.
日本語
よくあるミス
- インデックスは
0から始め、最後のアイテムは位置length - 1にあります。 - 終端を超えるとインデックスエラーになります。
English
Now you try
- Use an index, or a loop, on each list.
- Press Check answer to test your code.
日本語
あなたも試してみよう
- 各リストに対してインデックスまたはループを使用します。
- 回答を確認 を押してコードを試してください。
The list colours is set for you. Store its second item in a variable called second (remember Python counts from 0).
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Change the first item of the list nums to 99.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Use a loop to add up every number in nums and store the result in total (the answer is 43).
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。