【Python入門】第1回 PythonでHello Worldしよう

こんにちは
けいぞうです。

学生時代からプログラミングを始めて今年で11年目になります。
普段は会社員でシステムエンジニアとして仕事をしたり、
個人でアプリなどを作ったりしています。

今回はPythonシリーズの第1弾ということで、
Pythonで「Hello World」することをゴールとした記事になります。

スッキリわかるPython入門 (スッキリシリーズ)

この記事を読んで欲しい方
・プログラミングはちょっとやったことあるけど、Pythonは未経験

この記事を読むとできるようになること
・Pythonの開発環境が整う
・Pythonで「Hello World」のプログラムが作れる

この記事のシリーズを読むとできるようになること
・Pythonの基本的な考え方が分かって、プログラムを作れるようになる


※注意点
このシリーズでは、Windows OSでのPythonの開発を想定して説明を進めていきます。

Windowsユーザー以外の方は参考にならない部分があるかもしれません。
ご容赦くださいm(_ _)m

Pythonとは

Pythonはプログラミング言語の1つです。
パイソン」と読みます。

リリース:1991年
開発者:グイド・ヴァン・ロッサム
特徴:シンプル、可読性の高さ
公式サイト:https://www.python.org/

OSに依らない汎用性の高さがあり、
Webアプリデスクトップアプリゲーム機械学習などの
多くの分野で使用されているプログラミング言語です。

特に現在では、「機械学習ならPython」と言われるほど、機械学習の分野では必ずと言っていいほどPythonが使われています。

なぜ機械学習 = Pythonなのか?

今これから機械学習に取り組むというのなら
間違いなくPythonを選択するのが最善手です。

何故なら、
Pythonにはたくさんの機械学習向けのライブラリやフレームワークが存在しているからです。
これを使わない手はないでしょう。

では、
なぜPythonには機械学習向けのライブラリなどが多く存在しているのでしょうか。

Pythonという言語が本質的に機械学習に向いているから、
機械学習向けのライブラリが増えたのか、

機械学習向けのライブラリが増えたから、
機械学習にはPythonが良いと言われているのか。

鶏が先か、卵が先か。

すみません、私にはわかりません(笑)

その辺詳しい方、是非コメントで教えてください。

Pythonのインストール方法

前置きはこれくらいにして、
早速端末にPythonをインストールしていきましょう。

2020年9月現在、
Pythonの最新バージョンはPython3系になっています。
Python2系は既にサポートが終了する予定になっており、Python3上では正常に動作しないので、ご注意ください。

インストーラのダウンロード

まずPythonの公式ページに移動します
https://www.python.org/

Welcome to Python.org 
CD 
https://www.python.org 
Python 
python 
About 
Downloads 
up 
Docs 
Documentation 
to n 
pyPl 
Donate 
Jobs 
News 
Community 
Search 
GO 
Socialize 
Community 
Success Stories 
Events 
# Python 3: Fibonacci series 
def fib(n): 
while a < n: 
print(a, 
print() 
fib (1€00) 
end: ' 
Functions Defined 
The core of extensible programming is defining functions. 
Python allows mandatory and optional arguments, keyword 
arguments, and even arbitrary argument lists. More about 
defining functions in Python 3 
1 2 3 5 8 13 21 34 55 89 
144 233 377 610 987 
Python is a programming language that lets you work quickly 
and integrate systems more effectively. 222 Learn More 
Q.) Get Started 
Whether you're new to 
programming or an experienced 
developer, it's easy to learn and use 
Python. 
Start with our Beginner's Guide 
fill Latest News 
Download 
Python source code and installers 
are available for download for all 
versions! 
Latest: Python 
More 
Docs 
Documentation for Python's 
standard library, along with tutorials 
and guides, are available online. 
docs.python.org 
Upcoming Events 
Jobs 
Looking for work or have a Python 
related position that you're trying to 
hire for? Our relaunched 
community-run job board is the 
place to go. 
jobs.python.org 
More

メニューバーの「Downloads」にカーソルを当てて
「Windows」をクリックします。

Python 
python 
About 
# Python 3: Fi_- 
def fib(n) 
while 
pri 
print( 
fib (1€00) 
123581 
Docs 
Documentation 
pyPl 
Donate 
Jobs 
News 
Community 
Search 
Downloads 
All releases 
Source code 
Windows 
Mac OSX 
Other Platforms 
License 
GO 
Events 
ng functions. 
ments, keyword 
s. More about 
Socialize 
Community 
Success Stories 
Download for Windows 
Python 3.8.5 
Note that Python 3.5* cannot be used on Windows XP 
or earlier. 
Not the OS you are looking for? Python can be used on 
many operating systems and environments. 
View the full list of downloads. 
Alternative Implementations 
Python is a programming language that lets you work quickly 
and integrate systems more effectively. 222 Learn More

インストーラ様々ありますが、
今回は最新版ではなく、安定版のweb-based installerを使います。
Windows x86-64 web-based installer」をクリックします。

クリックすると、インストーラのダウンロードが始まります。

Python 
python 
About 
Python Downloads 
Downloads 
Docs 
Documentation 
pyPl 
Donate 
Jobs 
News 
Community 
Search 
GO 
Socialize 
Community 
Success Stories 
Events 
Python Releases for Windows 
Latest Python 3 Release - Python 3_8.S 
Latest Python 2 Release - Python 27.18 
Stable Releases 
python 3.8_6rc1 - sept. 8, 2020 
Note that Python 3.8.6rc1 cannot be used on Windows XP or earlier. 
• Download Windows help file 
• Download Windows x86-64 embeddable zip file 
• Download Windows x86-64 executable installer 
• Download Windows x86-64 web-based installer 
• Download Windows x86 embeddable zip file 
• Download Windows x86 executable installer 
• Download Windows x86 web-based installer 
python 3.510 - sept. S, 2020 
Note that Python 3.5.10 cannot be used on Windows XP or earlier. 
No files for this release. 
python 17, 2020 
Note that Python 3.7.9 cannot be used on Windows XP or earlier. 
python 3.9_orc2 - sept. 17, 2020 
• Download Windows help file 
• Download Windows x86-64 embeddable zip file 
• Download Windows x86-64 executable installer 
• Download Windows x86-64 web-based installer 
• Download Windows x86 embeddable zip file 
• Download Windows x86 executable installer 
• Download Windows x86 web-based installer 
python 3.5_10rc1 - Aug. 22, 2020 
No files for this release. 
python 3.9_orc1 - Aug. 11, 2020 
• Download Windows help file 
• Download Windows x86-64 embeddable zip file 
• Download Windows x86-64 executable installer 
• Download Windows x86-64 web-based installer

インストール

完了したら、
ダウンロードしたインストーラを実行してください。
以下のような画面が出てきます。

「Add Python 3.8 to PATH」にチェックを入れて
「Install Now」をクリックしてください。

Python 3.8.6rcI (64-bit) Setup 
python 
for 
windows 
Install Python 3.8.6rc1 (64-bit) 
Select Install Now to install Python with default settings, or choose 
Customize to enable or disable features. 
Install Now 
C: *Users 
AppData*LocaI on on 38 
Includes IDLE, pip and documentation 
Creates shortcuts and file associations 
-9 Customize installation 
Choose location and features 
Z] Install launcher for all users (recommended) 
Z] d Python 3.8 to PATH 
Cancel

インストールが始まります。

※インストールが始まる前に、「ユーザーアカウント制御」ダイアログが表示されたら「はい」をクリックして、インストールに進んでください。

Python 3.8.6rcI (64-bit) Setup 
Setup Progress 
Installing: 
Python 3.8.6rc1 Executables (64-bit) 
python 
for 
windows 
Cancel

インストールが正常に終了したら、
以下の画面が表示されます。
「Close」ボタンでインストール作業は完了です。

Python 3.8.6rcI (64-bit) Setup 
python 
for 
windows 
Setup was successful 
Special thanks to Mark Hammond, without whose years of 
freely shared Windows expertise, Python for Windows would 
still be Python for DOS. 
New to Python? Start with the online tutorial and 
documentation. 
See what's new in this release. 
Disable path length limit 
Changes your machine configuration to allow programs, including Python, to 
bypass the 260 character "MAX PATH" limitation. 
Close

インストール確認

Windowsボタンから「PowerShell」と打って、PowerShellを起動してください。

Windows PowerShell 
Windows PowerShell ISE 
Windows PowerShell (x86) 
Windows PowerShell ISE (x86) 
Developer PowerShell for VS 2019 
Debuggable Package Manager 
web 
p powershell 
- web 
powershell 
Windows PowerShell 
Windows PowerShel ISE

PowerShellが起動したら
python -V」と打ってみてください。
以下のようにバージョンが表示されたら、インストールは成功しています。

Windows PowerSheII 
PS python 
PS

※Pythonインストール前からPowerShellを起動済みだった場合は、上記の操作をしてもバージョンが表示されない場合があります。
その場合は、一度PowerShellを閉じて起動しなおしてから、
もう一度「python -V」を試してみてください。

Pythonの開発環境

Pythonの開発環境は、さまざま選択肢がありますが、
ここでは「Visual Studio Code(以下VSCode)」をおすすめします。

VSCodeの導入方法はこちらの記事で解説していますので、
参考にしてください。

VSCodeを導入したら、
VSCodeでPythonを快適に開発するための拡張機能(Extension)をインストールしましょう。

左側のExtensionボタンを押します。

File 
Edit Selection 
EXPLORER 
> OPEN EDITORS 
p 
v NO FOLDER OPENED 
O 
View 
Go 
Run 
Terminal Help 
Welcome 
You have not yet opened a folder. 
Open Folder 
You can also clone a repository from a 
URL To learn more about how to use 
git and source control in VS Code read 
our docs. 
Clone Repository

「Python」と検索してみてください。
一番上に表示されているのが、Microsoftが提供しているPythonのExtensionです。

File Edit Selection View 
EXTENSIONS: MARKETPLACE 
Go 
Run 
Terminal 
Help 
202&8_ 10B go 
Q 25M * as 
Linting, Debugging (multi-threaded, remote), Intellisense,... 
Microsoft 
Kite Autocomplete for Python and 0.1230 1.1M 
Code faster, stay in flow. Al-powered coding assistant fea... 
Python for VSCode 0.23 
Python language extension for vscode 
Thomas Haakon Townsend 
Python Extension Pack 1 _60 
01.1M 
Popular Visual Studio Code extensions for Python 
Don Jayamanne 
Python Test Explorer for Visual Stud... osn 181K 
Run your Python tests in the Sidebar of Visual Studio Code 
Little Fox Team 
304K

これを選択して、「Install」ボタンをクリックしてインストールしてください。

Extension: Python X 
Details 
Python 
ms-python. python 
Microsoft CD 25,050,392 Repository License 
„2020.&109390 
Linting, Debugging (multi-threaded, remote), Intellisense, Jupyter Notebooks, code formatting, refactoring, unit tests, snippets, and more. 
This extension is recommended based on the files you recently opened. Ignore Recommendation 
Feature Contributions Changelog 
Python extension for Visual Studio Code 
A Visual Studio Code extension with rich support for the Python language (for all actively supported versions of the language 2.7, including 
features such as IntelliSense linting, debugging, code navigation code formatting, Jupyter notebook support, refactoring, variable explorer, test explorer, 
snippets and more! 
Quick start 
St. 1 Install a supported version of Python on your system (note: that the system install of Python on macOS is not supported). 
St. 2. Install the Python extension for Visual Rudio Code. 
St. 3. Open or create a Python file and start coding! 
Set up your environment 
Select your Python interpreter by on the status bar

インストール中は「Installing」と表示されます。

Extension: Python X 
Details 
Python 
ms-python.python 
Microsoft 
Linting, Debugging (multi-threaded, remote), 
This extension is recommended based on the files. 
Feature Contributions Changelog 
Python extensi

しばらく待てば、インストールが完了します。
これでPythonを開発するための全ての準備が整いました!

Extension: Python X 
Details 
Python 
ms-python.python 
Microsoft CD 25,055,062 Repository License 
„2020.8.109390 
Linting, Debugging (multi-threaded, remote), Intellisense, Jupyter Notebooks, code formatting, refactoring, unit tests, snippets, and more. 
This extension is recommended based on the files you recently opened. Ignore Recommendation 
Feature Contributions Changelog 
Python extension for Visual Studio Code 
A Visual Studio Code extension with rich support for the P)Rhon language (for all actively supported versions of the language 2.7, including 
features such as IntelliSense, linting, debugging, code navigation code formatting, Jupyter notebook support, refactoring, variable explorer, test explorer, 
snippets and more! 
Quick start 
St. 1 Install a supported version of Python on your system (note: that the system install of Python on macOS is not supported). 
St. 2. Install the Python extension for Visual Rudio Code. 
St. 3. Open or create a Python file and start coding! 
Set up your environment 
Select your Python interpreter by on the status bar

Hello Worldを作ってみる

Hello Worldのプログラムを作っていきましょう。

左上のメニューの「File」から「New File」を選択

0+ΙΔΙ0 
Ν +μμμς+ΙΔΌ 
Ν+ΙΔΌ 
υηγ 09 ΜθΙΛ 
uadO 
uadO 
uadO 
"ΜΙΞ uadO 
ΜΟΡΙΙΙΛ% Μ,θΝ 
ΜθΝ 
ΙΙ0!ΡθΡς μη

上半分がソースを記述するウィンドウ、
下半分が結果などを表示するターミナルウィンドウです。

上半分のウィンドウに文字を打ち込んでコーディングしていきます。

Untitled-I 
PROBLEMS OUTPUT 
DEBUG CONSOLE 
TERMINAL 
2: Python

Pythonで標準出力に文字を表示させるには
print()関数を使います。

Hello World」と文字列を表示させたい場合は、
以下のようにシングルクォーテーションで囲います。

print(HelIo World') Untitled-I 
print('He110 World •51

書いたら、一旦保存しましょう。

左上メニューの「File」から「Save」
または「Ctrlキー + S」で保存できます。

適当にファイル名を決めて保存します。
ただし、Pythonファイルの拡張子は「.py」となるので、
ここでは「hello.py」というファイル名で保存します。

Plain Text ('.txt)

保存したら、VSCode君が「これはPythonのファイルだ!」と認識してくれるので、
良い感じに文字がハイライトされて、タブにもパイソンのマークが付きましたね。

hellupy X 
c > work > Script > TEST > hello.py 
print(' Hello World

実行するには、右上の再生ボタンを押します。

再生ボタンを押すと、
下のターミナルウィンドウに結果が表示されます。

PROBLEMS OUTPUT DEBUG CONSOLE 
TERMINAL 
PS C: & python c:/work/Script/TEST/he110.py 
Hello World

ちゃんと「Hello World」と表示されましたね!

おめでとうございます!
これであなたも今日からPythonプログラマーです!!(^^)/


ここまで読んでくださりありがとうございます。
質問、疑問、上手くいかない等がある方はコメントにお願いします。

次回、第2回は「変数」についてです。