リスト
5つの項目のリストのインデックスは0から4なので、menu[4]が最後の項目で、menu[5]はIndexErrorでプログラムを止めます。
1つの変数が献立全体を持てる
学校の給食の献立には複数の料理があり、買い物リストには複数の品物があります。料理ごとに変数をつくって、dish1、dish2、dish3とすることもできますが、献立が増えた途端に破綻します。リストは、いくつもの値を1つの変数に、順に保ちます。値を角括弧の中に、コンマで区切って書きます。
menu = ["rice", "seaweed soup", "bulgogi", "kimchi", "milk"]
print(menu)
print(len(menu))
print(menu[0], menu[-1])['rice', 'seaweed soup', 'bulgogi', 'kimchi', 'milk']
5
rice milkリストは、文字列と同じインデックス番号を使います。最初の項目は0、最後は-1で、menu[1:3]のようなスライスも使えます。len()は項目の数を返します。リスト全体をprintすると、Pythonは括弧を表示し、各文字列を単一引用符で囲むので、各項目がどこで始まりどこで終わるかが正確にわかります。
文字列と違って、リストは変えられる
文字列はその場で編集できませんが、リストはできます。インデックスに代入すると、項目が置き換わります。append()は末尾に項目を足し、remove()は最初に一致する値を削除し、pop()は最後の項目を取り出して返します。キーワードinは、値がリストにあるかを調べます。これらのメソッドはリスト自体を変えます。これが文字列のメソッドとの大きな違いです。
cart = ["eggs", "milk"]
cart.append("tofu")
cart[0] = "ramen"
print(cart)
cart.remove("milk")
last = cart.pop()
print(cart, last)
print("tofu" in cart)['ramen', 'milk', 'tofu']
['ramen'] tofu
False| この行のあと | cart |
|---|---|
| cart = ["eggs", "milk"] | ['eggs', 'milk'] |
| cart.append("tofu") | ['eggs', 'milk', 'tofu'] |
| cart[0] = "ramen" | ['ramen', 'milk', 'tofu'] |
| cart.remove("milk") | ['ramen', 'tofu'] |
| last = cart.pop() | ['ramen'](lastは'tofu') |
数のリストには、できあいの道具がある
数のリストには、sum()、min()、max()が名前どおりのことをします。sorted()は順に並べた新しいリストを返し、元のリストはそのままにします。メソッドのsort()はリスト自体を並べ替え、何も返しません。平均は合計を個数で割ったもので、len()のよい使い道です。
scores = [72, 95, 88, 64, 81]
print(sum(scores), min(scores), max(scores))
print(sum(scores) / len(scores))
print(sorted(scores))
print(scores)400 64 95
80.0
[64, 72, 81, 88, 95]
[72, 95, 88, 64, 81]2つの名前が同じリストを指すことがある
ほぼ誰もがつまずく部分です。b = aのあと、リストは2つではありません。名前が2つあるリストが1つあるのです。bを通して変えると、aにもその変化が見えます。aとbは同じリストだからです。数と文字列では、その場で変更できないので、これに気づきません。リストでは気づきます。別のコピーがほしければ、a.copy()かスライスa[:]で求めます。
a = ["rice", "soup"]
b = a
b.append("kimchi")
print(a)
c = a.copy()
c.append("egg")
print(a)
print(c)['rice', 'soup', 'kimchi']
['rice', 'soup', 'kimchi']
['rice', 'soup', 'kimchi', 'egg']リストとループは一緒に使うものです。30個の試験の点数のリストは、30行を別々に書かずに、プログラムがそのすべてを順に処理できて初めて役に立ち、それをするのが、ループの単元のforループです。それまでは、sum()、min()、max()、len()が、1回の呼び出しで、処理をしてくれます。
| リスト | [ ]の間に、コンマで区切って順に並べた値 |
| append(x) | リストの末尾にxを足す |
| remove(x) | xに等しい最初の項目を削除する |
| pop() | 最後の項目を取り出して返す |
| sorted(list) | 並べた新しいリスト。元はそのまま |
| list.sort() | リスト自体を並べ、Noneを返す |
| a.copy() | リストaの別のコピー |
- このコードは何を出力しますか。
nums = [10, 20, 30, 40] print(nums[2]) - このコードは何を出力しますか。
nums = [10, 20, 30, 40] print(nums[-1] + nums[0]) - このコードは何を出力しますか。
bag = ["pen"] bag.append("ruler") bag.append("eraser") print(len(bag)) - このコードは何を出力しますか。
x = [3, 1, 2] y = sorted(x) print(x, y)[3, 1, 2] [1, 2, 3][1, 2, 3] [1, 2, 3][1, 2, 3] [3, 1, 2][3, 1, 2] None
- このコードは何を出力しますか。
x = [3, 1, 2] y = x.sort() print(x, y)[3, 1, 2] [1, 2, 3][1, 2, 3] [1, 2, 3][1, 2, 3] NoneAn error
- このコードは何を出力しますか。
a = [1, 2] b = a b.append(3) print(a)[1, 2][1, 2, 3][3]An error
- このコードは何を出力しますか。
marks = [70, 90, 80] print(sum(marks) / len(marks)) - このコードは何を出力しますか。
stations = ["Seoul", "Yongsan", "Noryangjin", "Daebang"] print(stations[1:3]) - このコードは何を出力しますか。
stack = ["a", "b", "c"] top = stack.pop() print(top, len(stack)) - このコードは何を出力しますか。
print("milk" in ["eggs", "milk", "tofu"]) - プログラムが下のリストを出力するように、行を並べてください。出力
['homework', 'piano']
- 「最後の項目はmenu[len(menu)]。」 5項目ならインデックスは4で終わるので、menu[5]はIndexError: list index out of rangeで止まります。menu[-1]かmenu[len(menu) - 1]を使いましょう。
- 「menu = menu.append(x)は、新しいリストを保つ。」 append()はリストを変えてNoneを返すので、この行はリスト全体をNoneに置き換えます。menu.append(x)だけを書きましょう。
- 「b = aは、リストのバックアップをくれる。」 同じリストに2つ目の名前を与えるだけです。どちらの名前を通した変更も、両方に現れます。本当のコピーにはb = a.copy()を使いましょう。
- リストは値を順に持つ:[a, b, c]。
- インデックスとスライスは文字列と同じ。最後のインデックスはlen - 1。
- append、remove、pop、インデックスへの代入は、リスト自体を変える。
- sorted()は新しいリストを返す。sort()はリストを変えてNoneを返す。
- b = aは1つのリストを共有する。別のリストにはa.copy()を使う。
このコースの出典 (6)
- Python Software Foundation, The Python Tutorial for Python 3.12 (docs.python.org/3.12/tutorial: 2 Using the Python Interpreter, 3 An Informal Introduction to Python (numbers, text, lists), 4 More Control Flow Tools (if, for, range, break and continue, defining functions, default argument values, 4.10 coding style: 4-space indentation), 5 Data Structures (list methods, 5.5 dictionaries, insertion order), 8 Errors and Exceptions (syntax errors, exceptions, reading the traceback, handling exceptions), 15 Floating-Point Arithmetic (0.1 + 0.2))
- Built-in Functions (docs.python.org/3.12/library/functions.html: print, input, len, int, float, str, type, round (round half to even: round(0.5) and round(-0.5) are 0, round(1.5) is 2), range, sum, min, max, sorted)
- Built-in Types (str methods upper, lower, replace, strip; dict.get; list.append, pop, remove, sort returning None)
- General Python FAQ (docs.python.org/3.12/faq/general.html: Guido van Rossum began Python in the 1989 Christmas holidays, posted it to USENET in February 1991, named it after Monty Python's Flying Circus)
- Curriculum for the Korean pages: 교육부 고시 제2022-33호 [별책 10] 정보과 교육과정 (PDF read 2026-10-19: 중학교 정보 [9정03-05] 리스트 등 데이터를 순차적으로 저장하는 구조, [9정03-06] 논리 연산과 중첩 제어 구조, [9정03-07] 함수 활용과 디버거로 오류 수정, [9정03-08] 실생활 문제를 프로그래밍으로 해결, 적용 시 고려 사항 「학생의 수준을 고려하여 적합한 프로그래밍 언어를 선정」; 고등학교 정보 [12정03-04] 자료형, [12정03-05] 표준입출력, [12정03-07] 제어 구조)
- Bug story only: Smithsonian National Museum of American History, Log Book With Computer Bug, 1947, Harvard Mark II, "first actual case of bug being found", probably not Hopper's log book (americanhistory.si.edu/collections/object/nmah_334663, read 2026-10-19)
確認日: 2026-10-19