Skip to content

Commit 404de61

Browse files
committed
python→python-repl
1 parent 0f8dc51 commit 404de61

6 files changed

Lines changed: 47 additions & 51 deletions

File tree

public/docs/python-4.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Pythonの条件分岐は`if``elif`(else ifの略)、`else`を使って記述します。C言語やJavaのような波括弧`{}`は使わず、**コロン`:`とインデント(通常は半角スペース4つ)**でコードブロックを表現するのが最大の特徴です。
88

9-
```python
9+
```python-repl
1010
>>> score = 85
1111
>>> if score >= 90:
1212
... print('優')
@@ -22,7 +22,7 @@ Pythonの条件分岐は`if`、`elif`(else ifの略)、`else`を使って記
2222

2323
条件式に`and``or``not`といった論理演算子も使用できます。
2424

25-
```python
25+
```python-repl
2626
>>> temp = 25
2727
>>> is_sunny = True
2828
>>> if temp > 20 and is_sunny:
@@ -35,7 +35,7 @@ Pythonの条件分岐は`if`、`elif`(else ifの略)、`else`を使って記
3535

3636
Pythonの`for`ループは、他の言語の`for (int i = 0; i < 5; i++)`といったカウンタ変数を使うスタイルとは少し異なります。リストやタプル、文字列などの**イテラブル(反復可能)オブジェクト**から要素を1つずつ取り出して処理を実行します。これは、Javaの拡張for文やC\#`foreach`に似ています。
3737

38-
```python
38+
```python-repl
3939
>>> fruits = ['apple', 'banana', 'cherry']
4040
>>> for fruit in fruits:
4141
... print(f"I like {fruit}")
@@ -49,7 +49,7 @@ I like cherry
4949

5050
決まった回数のループを実行したい場合は、`range()`関数が便利です。`range(n)`は0からn-1までの連続した数値を生成します。
5151

52-
```python
52+
```python-repl
5353
>>> for i in range(5):
5454
... print(i)
5555
...
@@ -64,7 +64,7 @@ I like cherry
6464

6565
ループ処理の中で、要素のインデックス(番号)と値の両方を使いたい場合があります。そのような時は`enumerate()`関数を使うと、コードが非常にスッキリします。これは非常にPythonらしい書き方の一つです。
6666

67-
```python
67+
```python-repl
6868
>>> fruits = ['apple', 'banana', 'cherry']
6969
>>> for i, fruit in enumerate(fruits):
7070
... print(f"Index: {i}, Value: {fruit}")
@@ -78,7 +78,7 @@ Index: 2, Value: cherry
7878

7979
`while`ループは、指定された条件が`True`である間、処理を繰り返します。ループを途中で抜けたい場合は`break`を、現在の回の処理をスキップして次の回に進みたい場合は`continue`を使用します。
8080

81-
```python
81+
```python-repl
8282
>>> n = 0
8383
>>> while n < 5:
8484
... print(n)
@@ -95,7 +95,7 @@ Index: 2, Value: cherry
9595

9696
関数は`def`キーワードを使って定義します。ここでもコードブロックはコロン`:`とインデントで示します。値は`return`キーワードで返します。
9797

98-
```python
98+
```python-repl
9999
>>> def greet(name):
100100
... """指定された名前で挨拶を返す関数""" # これはDocstringと呼ばれるドキュメント文字列です
101101
... return f"Hello, {name}!"
@@ -113,7 +113,7 @@ Pythonの関数は、非常に柔軟な引数の渡し方ができます。
113113
* **キーワード引数 (Keyword Arguments):** `引数名=値`の形式で渡します。順序を問わないため、可読性が向上します。
114114
* **デフォルト引数値 (Default Argument Values):** 関数を定義する際に引数にデフォルト値を設定できます。呼び出し時にその引数が省略されると、デフォルト値が使われます。
115115

116-
```python
116+
```python-repl
117117
>>> def describe_pet(animal_type, pet_name, owner_name="Taro"):
118118
... print(f"私には {animal_type} がいます。")
119119
... print(f"名前は {pet_name} で、飼い主は {owner_name} です。")
@@ -145,7 +145,7 @@ Pythonの関数は、非常に柔軟な引数の渡し方ができます。
145145

146146
任意の数の**位置引数**をタプルとして受け取ります。慣習的に`args`という名前が使われます。
147147

148-
```python
148+
```python-repl
149149
>>> def sum_all(*numbers):
150150
... print(f"受け取ったタプル: {numbers}")
151151
... total = 0
@@ -165,7 +165,7 @@ Pythonの関数は、非常に柔軟な引数の渡し方ができます。
165165

166166
任意の数の**キーワード引数**を辞書として受け取ります。慣習的に`kwargs` (keyword arguments) という名前が使われます。
167167

168-
```python
168+
```python-repl
169169
>>> def print_profile(**user_info):
170170
... print(f"受け取った辞書: {user_info}")
171171
... for key, value in user_info.items():
@@ -184,7 +184,7 @@ city: Tokyo
184184

185185
構文: `lambda 引数: 式`
186186

187-
```python
187+
```python-repl
188188
# 通常の関数で2つの数を足す
189189
>>> def add(x, y):
190190
... return x + y

public/docs/python-5.md

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
1. **ファイルの作成**:
1515
お使いのテキストエディタ(VS Code、サクラエディタ、メモ帳など何でも構いません)を開き、以下のコードを記述してください。そして、`hello.py` という名前で保存します。ファイルの拡張子は必ず `.py` にしてください。
1616

17-
```python
18-
# hello.py
17+
```python:hello.py
1918
message = "Hello, Python Script!"
2019
print(message)
2120
print(f"2 + 3 = {2 + 3}")
@@ -47,7 +46,7 @@
4746

4847
モジュールを利用するには `import` 文を使います。Pythonには多くの便利なモジュールが標準で用意されています(これらを**標準ライブラリ**と呼びます)。例えば、数学的な計算を行う `math` モジュールをREPLで使ってみましょう。
4948

50-
```python
49+
```python-repl
5150
>>> # mathモジュールをインポート
5251
>>> import math
5352
>>>
@@ -62,7 +61,7 @@
6261

6362
* **`from ... import ...`**: モジュールから特定の関数や変数だけを取り込む
6463

65-
```python
64+
```python-repl
6665
>>> from math import pi, sqrt
6766
>>>
6867
>>> print(pi) # 直接piを参照できる
@@ -73,7 +72,7 @@
7372
7473
* **`as` (別名)**: モジュールに別名をつけて利用する
7574
76-
```python
75+
```python-repl
7776
>>> import math as m
7877
>>>
7978
>>> print(m.pi)
@@ -91,8 +90,7 @@
9190
1. **`utils.py` の作成**:
9291
まず、便利な関数をまとめた `utils.py` というファイルを作成します。
9392
94-
```python
95-
# utils.py
93+
```python:utils.py
9694
9795
def say_hello(name):
9896
"""指定された名前で挨拶を返す"""
@@ -122,8 +120,7 @@
122120
2. **`main.py` からの利用**:
123121
次に、`utils.py` と同じディレクトリに `main.py` を作成し、`utils` モジュールをインポートして使います。
124122
125-
```python
126-
# main.py
123+
```python:main.py
127124
128125
# 自作のutilsモジュールをインポート
129126
import utils
@@ -164,8 +161,7 @@ my_project/
164161
165162
`main.py` からこれらのモジュールをインポートするには、`パッケージ名.モジュール名` のように記述します。
166163
167-
```python
168-
# main.py
164+
```python:main.py
169165
170166
# パッケージ内のモジュールをインポート
171167
from my_app import services
@@ -190,7 +186,7 @@ Pythonの大きな魅力の一つは、その「**バッテリー同梱 (Batteri
190186

191187
また、REPLの `help()``dir()` を使うと、モジュールの内容を簡単に確認できます。
192188

193-
```python
189+
```python-repl
194190
>>> import datetime
195191
>>> # datetimeモジュールが持つ属性や関数のリストを表示
196192
>>> dir(datetime)
@@ -213,7 +209,7 @@ class date(builtins.object)
213209

214210
* **`os`**: OSとの対話。ファイルやディレクトリの操作など。
215211

216-
```python
212+
```python-repl
217213
>>> import os
218214
>>>
219215
>>> # カレントディレクトリのファイル一覧を取得
@@ -228,7 +224,7 @@ class date(builtins.object)
228224
229225
* **`sys`**: Pythonインタプリタに関する情報。コマンドライン引数など。
230226
231-
```python
227+
```python-repl
232228
>>> import sys
233229
>>>
234230
>>> # Pythonのバージョンを表示
@@ -240,7 +236,7 @@ class date(builtins.object)
240236
241237
* **`datetime`**: 日付や時刻の操作。
242238
243-
```python
239+
```python-repl
244240
>>> import datetime
245241
>>>
246242
>>> # 現在の日時を取得
@@ -255,7 +251,7 @@ class date(builtins.object)
255251
256252
* **`json`**: JSON形式のデータの操作。
257253
258-
```python
254+
```python-repl
259255
>>> import json
260256
>>>
261257
>>> # Pythonの辞書型データ

public/docs/python-6.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Pythonでは、`class`キーワードを使ってクラスを定義します。J
88

99
クラスを定義したら、関数を呼び出すように`クラス名()`と書くことで、そのクラスの**インスタンス**(オブジェクト)を生成できます。
1010

11-
```python
11+
```python-repl
1212
>>> # 中身が何もない、最もシンプルなクラスを定義
1313
>>> class User:
1414
... pass
@@ -27,7 +27,7 @@ Pythonでは、`class`キーワードを使ってクラスを定義します。J
2727

2828
`__init__`メソッドの最初の引数には、慣習的に`self`という名前を付けます。この`self`は、生成されるインスタンス自身を指す参照で、JavaやJavaScriptの`this`と同じ役割を果たします。`self`を通じて、インスタンスに固有のデータを格納する**インスタンス変数**を定義します。
2929

30-
```python
30+
```python-repl
3131
>>> class Dog:
3232
... # インスタンス生成時に呼び出されるコンストラクタ
3333
... def __init__(self, name, age):
@@ -56,7 +56,7 @@ Pythonのクラスには2種類の変数があります。
5656

5757
<!-- end list -->
5858

59-
```python
59+
```python-repl
6060
>>> class Cat:
6161
... # このクラスから作られるインスタンス全てで共有されるクラス変数
6262
... species = "ネコ科"
@@ -94,7 +94,7 @@ Pythonのクラスには2種類の変数があります。
9494

9595
メソッドを定義する際も、最初の引数には必ず`self`を指定する必要があります。これにより、メソッド内から`self`を通じてインスタンス変数にアクセスできます。
9696

97-
```python
97+
```python-repl
9898
>>> class Dog:
9999
... def __init__(self, name):
100100
... self.name = name
@@ -119,7 +119,7 @@ Pythonでは`class 子クラス名(親クラス名):`という構文で継承を
119119

120120
子クラスで親クラスのメソッドを上書き(**オーバーライド**)したり、`super()`関数を使って親クラスのメソッドを呼び出したりすることもできます。特に、子クラスの`__init__`で親クラスの`__init__`を呼び出すのは一般的なパターンです。
121121

122-
```python
122+
```python-repl
123123
>>> # 親クラス
124124
>>> class Animal:
125125
... def __init__(self, name):
@@ -172,7 +172,7 @@ Animalの__init__が呼ばれました
172172

173173
<!-- end list -->
174174

175-
```python
175+
```python-repl
176176
>>> class Person:
177177
... def __init__(self, name, age):
178178
... self.name = name

public/docs/python-7.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Pythonでファイルを操作するには、まず組み込み関数の **`open
1717

1818
<!-- end list -->
1919

20-
```python
20+
```python-repl
2121
>>> # 'w' モードでファイルを開く(または新規作成する)
2222
>>> f = open('spam.txt', 'w', encoding='utf-8')
2323
>>> f
@@ -37,7 +37,7 @@ Pythonでファイルを操作するには、まず組み込み関数の **`open
3737

3838
**`write()`** メソッドは、文字列をファイルに書き込みます。このメソッドは書き込んだ文字数を返します。
3939

40-
```python
40+
```python-repl
4141
>>> f = open('test.txt', 'w', encoding='utf-8')
4242
>>> f.write('こんにちは、世界!\n')
4343
9
@@ -58,7 +58,7 @@ Pythonでファイルを操作するには、まず組み込み関数の **`open
5858

5959
<!-- end list -->
6060

61-
```python
61+
```python-repl
6262
>>> # 先ほど書き込んだファイルを読み込む
6363
>>> f = open('test.txt', 'r', encoding='utf-8')
6464
>>> content = f.read()
@@ -86,7 +86,7 @@ Pythonでファイルを操作するには、まず組み込み関数の **`open
8686

8787
**`with`** 文のブロックを抜けると、ファイルオブジェクトは自動的に `close()` されます。エラーが発生した場合でも同様です。これは「コンテキストマネージャ」という仕組みによって実現されており、ファイル操作の標準的な方法です。
8888

89-
```python
89+
```python-repl
9090
>>> # with文を使った書き込み
9191
>>> with open('spam.txt', 'w', encoding='utf-8') as f:
9292
... f.write('withブロックを使っています。\n')
@@ -118,7 +118,7 @@ withブロックを使っています。
118118

119119
<!-- end list -->
120120

121-
```python
121+
```python-repl
122122
>>> import json
123123
124124
>>> # 書き込むデータ(Pythonの辞書)
@@ -156,7 +156,7 @@ withブロックを使っています。
156156

157157
**`csv.writer()`** を使ってライターオブジェクトを作成し、**`writerow()`** (1行) や **`writerows()`** (複数行) メソッドでデータを書き込みます。
158158

159-
```python
159+
```python-repl
160160
>>> import csv
161161
162162
>>> # 書き込むデータ(リストのリスト)
@@ -178,7 +178,7 @@ withブロックを使っています。
178178

179179
**`csv.reader()`** を使ってリーダーオブジェクトを作成します。このオブジェクトをループで回すことで、1行ずつリストとしてデータを取得できます。
180180

181-
```python
181+
```python-repl
182182
>>> import csv
183183
184184
>>> with open('scores.csv', 'r', newline='', encoding='-utf-8') as f:

public/docs/python-8.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
例えば、`0` で割り算をすると `ZeroDivisionError` という例外が発生します。
1010

11-
```python
11+
```python-repl
1212
>>> 10 / 0
1313
Traceback (most recent call last):
1414
File "<stdin>", line 1, in <module>
@@ -17,7 +17,7 @@ ZeroDivisionError: division by zero
1717

1818
このエラーを `try...except` で捕捉してみましょう。
1919

20-
```python
20+
```python-repl
2121
>>> try:
2222
... result = 10 / 0
2323
... except ZeroDivisionError:
@@ -38,7 +38,7 @@ ZeroDivisionError: division by zero
3838

3939
エラーの種類ごとに異なる処理を行いたい場合に適しています。
4040

41-
```python
41+
```python-repl
4242
>>> def calculate(a, b):
4343
... try:
4444
... a = int(a)
@@ -62,7 +62,7 @@ ZeroDivisionError: division by zero
6262

6363
複数の例外に対して同じ処理を行いたい場合に便利です。
6464

65-
```python
65+
```python-repl
6666
>>> def calculate_v2(a, b):
6767
... try:
6868
... a = int(a)
@@ -86,7 +86,7 @@ ZeroDivisionError: division by zero
8686

8787
例えば、負の値を受け付けない関数を考えてみましょう。
8888

89-
```python
89+
```python-repl
9090
>>> def process_positive_number(num):
9191
... if num < 0:
9292
... raise ValueError("負の値は処理できません。")
@@ -112,7 +112,7 @@ ValueError: 負の値は処理できません。
112112

113113
すべての節を使った例を見てみましょう。
114114

115-
```python
115+
```python-repl
116116
>>> def divider(a, b):
117117
... print(f"--- {a} / {b} の計算を開始します ---")
118118
... try:

0 commit comments

Comments
 (0)