Published Date : 2020年3月25日14:49

【Part 1】Pythonインタプリタを使用する
【Part 1】Using the Python Interpreter


This blog has an English translation


YouTubeにアップした動画、「【Python】Part 1 - 一分間の3Dアニメーションで理解するPythonの基礎」の補足説明の記事です。

Here's a little more about the "【Python】Part 1 - Understand the basics of Python with a one-minute 3D animation" video I uploaded to YouTube.

シリーズの第1部の内容は、Pythonインタプリタを使用するです。

The first part of the series uses the Python interpreter.

全体の説明は動画に任せるとして、補足が必要だろうと思われる部分を説明していきます。

I'll leave the entire explanation to the video, but I'll explain the parts that I think need to be supplemented.


目次

Table of Contents




Pythonインタープリタ
Python Interpreter




Pythonを動かす
Running Python interactively

まず、Windowsユーザーならコマンドプロンプト、macかLinuxユーザーならターミナルを立ち上げます。

First, Start a command prompt for Windows users, or a terminal for Mac or Linux users.


windows


Responsive image


Mac


Responsive image

そして、コンソール画面に「Python」と打ち込みます。PythonのインストールとPathの通し方はこの記事を参考にしてくだちぃ。

Type "python" in the console. See this blog post for Python installation and Path.

(your user name) $ python

するとコンソール画面に以下のような文字が表れて、「>>>」の後にPythonを使えるようになります。

You should see something like the following on your console screen, and you should be able to use Python after [>>>].

Python 3.8.2 (default, Jan 1 2020, 00:00:00)
[GCC 4.8.2] on your os
Type "help", "copyright", "credits" or "license" for more information.
>>>

試しに以下のスクリプトを入力してみましょう。

Try typing the following script.

>>> 日本語 = True
>>> あいさつ = "こんにちは"
>>> if 日本語:
...     print(あいさつ)
... 
こんにちは

>>> english = True
>>> say_goodbye = 'goodbye'
>>> if english:
...     print(say_goodbye)
... 
goodbye

ちなみにPythonは文のブロックを「空白」で認識しています。忘れずにIf文の後にTABかスペースを打ってから、Print関数を入力してください。

By the way, Python recognizes blocks of statements in "Blank". Before you use the print function, be sure to press TAB or SPACEBAR after the If statement.




スクリプトで使われている「IF」と「True」
[IF] and [True] used in the script



先ほど使われていた「IF」と「True」という文と変数の簡単な説明です。

Brief description of "If statement" and variable "True" above.

このスクリプトの場合、IF文は横の変数が「True」か「False」かを判定します。

In this script, the If statement determines whether the variable next to it is "True" or "False".

この文の場合、変数「日本語」または「english」には初めから「Boolean変数」として「True」が格納されているので、 IF文の次に記述されているPrint関数を実行します。

Because this statement assigns "True" as "Boolean variable" to the "日本語(Japanese)" or "english" variables, it immediately evaluates to "True" and executes the Print function that follows the If statement.

もしこの変数が「False」なら実行しません。しかし、もしこの文の最初に「not 変数」のように「not」がついていると、真偽値は逆になります。 つまり変数が「False」の時、「if not 変数」はTrueになります。

Do not execute if this variable is "False". However, if the statement begins with a "not" such as "not e(variable)", the truth value is reversed. That is, if the "e(variable)" is "False", then "if not e(variable)" will be true.

>>> e = False
>>> if not e:
...     print(str(e))
...
False

さらに、[if文]は[else]と[elif]で処理を分岐させることができます。

In addition, [if statement] can branch processing between [else] and [else if].

試しに、上で紹介したスクリプトに[else]と[elif]をつけて挙動を確かめてみましょう。

As a test, try adding [else] and [elif] to the script above to see how it works.

>>> 日本語 = False
>>> あいさつ = "hi"
>>> if not 日本語:
...     print(あいさつ)
... 
hi

>>> english = False
>>> say_goodbye = 'さようなら'
>>> if not english:
...     print(say_goodbye)
... 
さようなら
>>> さようなら = "goodbye"
>>> hello = "こんにちは"
>>> if english:
...   print(say_goodbye)
... elif 日本語:
...   print(あいさつ)
... else:
...   print("you say " + さようなら + " and I say " + hello)
...
you say goodbye and I say こんにちは
>>>
>>> さようなら = "goodbye"
>>> hello = "こんにちは"
>>> if english:
...   print(say_goodbye)
... elif not 日本語:
...   print(さようなら)
... else:
...   print("you say " + さようなら + " and I say " + hello)
...
goodbye
>>>


それでは次回もPythonの基礎について説明していきます。

Next time, I'll continue to cover the basics of Python.





See You Next Page!