Pythonを対話モードで起動する


無料Linux入門マニュアル無料ダウンロード

今だけ2,200円のLinux入門PDFマニュアルが【数量限定】で無料ダウンロードできます。
Linux入門マニュアル無料ダウンロードはこちらをクリック

CentOS(Linux)環境でPythonを対話モード(インタプリタともいいます)で起動するには、
コマンドプロンプトでPythonと実行します。

$ python
Python 3.6.8 (default, Aug 7 2019, 17:28:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>


試しに簡単な計算をしてみましょう。
「10*10-1」と入力してEnterキーを押下します。

$ python
Python 3.6.8 (default, Aug 7 2019, 17:28:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 10*10-1
99  ←計算の実行結果が表示されます。


続けてもう少しプログラミングらしい計算をしてみましょう。
変数を使って処理を実行します。
次のように入力してEnterキーを押下します。

$ python
Python 3.6.8 (default, Aug 7 2019, 17:28:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 10*10-1
99  ←計算の実行結果が表示されます。
>>> num1 = 10
>>> num2 = 10
>>> num3 = 1
>>> num1 * num2 - num3
99  ←計算の実行結果が表示されます。


Pythonは、単純計算だけではなく、変数や条件分岐、繰り返し処理などを
使用したプログラミングをスクリプトファイルをなどに記述せず
対話で実行することができます。



Pythonを対話モードを終了する

CentOS(Linux)環境でPythonを対話モードを終了するには、
「Ctrl + d」を実行します。

$ python
Python 3.6.8 (default, Aug 7 2019, 17:28:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 10*10-1
99  ←計算の実行結果が表示されます。
>>> num1 = 10
>>> num2 = 10
>>> num3 = 1
>>> num1 * num2 - num3
99  
>>>  ←「Ctrl + d」を実行します。
$  ←コマンドプロンプトに戻ります。


Pythonの対話モードでヘルプを参照する

Pythonの対話モードを使用中にヘルプを参照したい場合は、 次のように実行してください。 例では、for文についてのヘルプを参照します。

$ python
Python 3.6.8 (default, Aug 7 2019, 17:28:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help('for')


画面が切り替わり、forのヘルプが参照できます。

$ python
Python 3.6.8 (default, Aug 7 2019, 17:28:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
The "for" statement
*******************

The "for" statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object:

for_stmt ::= "for" target_list "in" expression_list ":" suite
["else" ":" suite]

The expression list is evaluated once; it should yield an iterable
object. An iterator is created for the result of the
"expression_list". The suite is then executed once for each item
provided by the iterator, in the order returned by the iterator. Each
item in turn is assigned to the target list using the standard rules
for assignments (see Assignment statements), and then the suite is
executed. When the items are exhausted (which is immediately when the
sequence is empty or an iterator raises a "StopIteration" exception),
the suite in the "else" clause, if present, is executed, and the loop
terminates.

A "break" statement executed in the first suite terminates the loop
without executing the "else" clause’s suite. A "continue" statement
executed in the first suite skips the rest of the suite and continues
with the next item, or with the "else" clause if there is no next
item.

The for-loop makes assignments to the variables(s) in the target list.
This overwrites all previous assignments to those variables including
those made in the suite of the for-loop:

for i in range(10):
print(i)
i = 5 # this will not affect the for-loop
# because i will be overwritten with the next
# index in the range

Names in the target list are not deleted when the loop is finished,
but if the sequence is empty, they will not have been assigned to at
all by the loop. Hint: the built-in function "range()" returns an
iterator of integers suitable to emulate the effect of Pascal’s "for i
:= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]".

Note: There is a subtlety when the sequence is being modified by the
loop (this can only occur for mutable sequences, e.g. lists). An
: ←「:」が表示される場合は、続きがあるのでスペースキーを押下します。

internal counter is used to keep track of which item is used next,
and this is incremented on each iteration. When this counter has
reached the length of the sequence the loop terminates. This means
that if the suite deletes the current (or a previous) item from the
sequence, the next item will be skipped (since it gets the index of
the current item which has already been treated). Likewise, if the
suite inserts an item in the sequence before the current item, the
current item will be treated again the next time through the loop.
This can lead to nasty bugs that can be avoided by making a
temporary copy using a slice of the whole sequence, e.g.,

for x in a[:]:
if x < 0: a.remove(x)

Related help topics: break, continue, while
(END) ←「(END)」が表示されたら、「q」キーを入力します。

>>> help('for')

>>>  ←pyhtonの対話モードに戻ります。


無料Linux入門マニュアル無料ダウンロード

今だけ2,200円のLinux入門PDFマニュアルが【数量限定】で無料ダウンロードできます。
Linux入門マニュアル無料ダウンロードはこちらをクリック


<<関連記事>>
・Pythonでコメントを記述する
・CentOS(Linux)にPythonをインストール(Python2/Python3)

Linux入門マニュアル無料ダウンロード