Skip to content

Commit ba554d8

Browse files
authored
Merge pull request #7 from python-ed-open-doc/feature/ryu22e-start-up-and-interpreter
「Pythonをコマンドから起動、Pythonの対話モード」を追加
2 parents 0c34bd0 + d698017 commit ba554d8

7 files changed

+96
-1
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -1 +1,96 @@
1-
# Pythonをコマンドから起動・Pythonの対話モード
1+
(start-up-and-interpreter)=
2+
3+
# Pythonをコマンドから起動、Pythonの対話モード
4+
5+
ここでは、Pythonをコマンドから起動する方法と、Pythonの対話モードの使い方について解説します。
6+
7+
(run-python-for-mac)=
8+
## Pythonをコマンドから起動
9+
10+
### macOS
11+
Finderを起動し、「アプリケーション」-「ユーティリティ」を開き、「ターミナル」をダブルクリックしてターミナルを起動します。
12+
13+
![Finderで「アプリケーション」-「ユーティリティ」を開く](./images/run-python-for-mac-1.png)
14+
*Finderで「アプリケーション」-「ユーティリティ」を開く*
15+
16+
![ターミナルを起動](./images/run-python-for-mac-2.png)
17+
*ターミナルを起動*
18+
19+
ターミナル上で`$`より右に`python{バージョン番号} -V`というコマンドを入力すると、インストールされたPythonのバージョンを確認できます。
20+
たとえば、Python 3.9がインストールされている場合は`python3.9 -V`と入力します。
21+
22+
![インストールされたPythonのバージョンを確認](./images/show-python-version-for-mac.png)
23+
*インストールされたPythonのバージョンを確認*
24+
25+
このとき、`python -V`のようにpythonコマンドの後ろの数字を省略しないでください。
26+
macOSにはデフォルトでPython 2.7系がインストールされているため、このコマンドではインストールしたものとは別のPythonが起動されてしまいます。
27+
28+
### Windows
29+
まず、Windows PowerShellを起動します。画面左下の検索ボックスを使うと素早く起動できます。
30+
31+
![検索ボックスからWindows PowerShellを起動](./images/run-python-for-win-1.png)
32+
*検索ボックスからWindows PowerShellを起動*
33+
34+
検索ボックスを使わない場合は画面左下のスタートボタンをクリックしてからWindows PowerShellのアイコンをクリックしてください。Windows PowerShellのアイコンは、名前が「W」で始まるアプリケーションのフォルダの中にあります。
35+
36+
![スタートボタンからWindows PowerShellを起動](./images/run-python-for-win-2.png)
37+
*スタートボタンからWindows PowerShellを起動*
38+
39+
ターミナル上で`>`より右に`python -V`というコマンドを入力すると、インストールされたPythonのバージョンを確認できます。
40+
41+
![インストールされたPythonのバージョンを確認](./images/show-python-version-for-win.png)
42+
*インストールされたPythonのバージョンを確認*
43+
44+
## Pythonの対話モード
45+
46+
{ref}`run-python-for-mac`と同様の方法でターミナルを起動し、`python{バージョン番号}`というコマンドを入力します。
47+
たとえば、Python 3.9がインストールされている場合は`python3.9`と入力します。
48+
49+
上記コマンドを実行するとPythonの対話モードが起動します。
50+
51+
macOSの場合、画面が以下の状態になります。
52+
53+
```python
54+
Python 3.9.7 (default, Sep 3 2021, 12:36:14)
55+
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
56+
Type "help", "copyright", "credits" or "license" for more information.
57+
>>>
58+
```
59+
60+
Windowsの場合、画面が以下の状態になります。
61+
62+
```python
63+
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
64+
Type "help", "copyright", "credits" or "license" for more information.
65+
>>>
66+
```
67+
68+
`>>>`より右にPythonのコードを入力することができます。
69+
70+
```python
71+
>>> print("Hello World!") # 「Hello World!」を出力
72+
Hello World!
73+
>>> a = 1 + 2 + 3 # 足し算の結果を変数に代入
74+
>>> print(a) # 変数の内容を出力
75+
6
76+
```
77+
78+
コードが複数行の場合は改行するたびに先頭に`...`が出力されます。
79+
`...`に何も入力しない状態で改行すると、それまでに入力したコードが実行されます。
80+
81+
```python
82+
>>> for i in range(5): # 0から4までを出力
83+
... print(i)
84+
...
85+
0
86+
1
87+
2
88+
3
89+
4
90+
```
91+
92+
対話モードは以下のいずれかの方法で終了させることができます。
93+
94+
* `quit()`関数を実行
95+
* `Ctrl + D`を入力(macOSのみ)
96+
* `Ctrl + Z`を入力してからEnterキーを入力(Windowsのみ)

0 commit comments

Comments
 (0)