こんにちは
けいぞうです。
学生時代からプログラミングを始めて今年で11年目になります。
普段は会社員でシステムエンジニアとして仕事をしたり、
個人でアプリなどを作ったりしています。
今回はPythonシリーズの第1弾ということで、
Pythonで「Hello World」することをゴールとした記事になります。
★この記事を読んで欲しい方★
・プログラミングはちょっとやったことあるけど、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/)

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

インストーラ様々ありますが、
今回は最新版ではなく、安定版のweb-based installerを使います。
「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](http://trend-desk.com/wp-content/uploads/2020/09/image-8.png)
インストールが始まります。
※インストールが始まる前に、「ユーザーアカウント制御」ダイアログが表示されたら「はい」をクリックして、インストールに進んでください。

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

インストール確認
Windowsボタンから「PowerShell」と打って、PowerShellを起動してください。

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

※Pythonインストール前からPowerShellを起動済みだった場合は、上記の操作をしてもバージョンが表示されない場合があります。
その場合は、一度PowerShellを閉じて起動しなおしてから、
もう一度「python -V」を試してみてください。
Pythonの開発環境
Pythonの開発環境は、さまざま選択肢がありますが、
ここでは「Visual Studio Code(以下VSCode)」をおすすめします。
VSCodeの導入方法はこちらの記事で解説していますので、
参考にしてください。
VSCodeを導入したら、
VSCodeでPythonを快適に開発するための拡張機能(Extension)をインストールしましょう。
左側のExtensionボタンを押します。

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

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

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

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

Hello Worldを作ってみる
Hello Worldのプログラムを作っていきましょう。
左上のメニューの「File」から「New File」を選択

上半分がソースを記述するウィンドウ、
下半分が結果などを表示するターミナルウィンドウです。
上半分のウィンドウに文字を打ち込んでコーディングしていきます。

Pythonで標準出力に文字を表示させるには
print()関数を使います。
「Hello World」と文字列を表示させたい場合は、
以下のようにシングルクォーテーションで囲います。

書いたら、一旦保存しましょう。
左上メニューの「File」から「Save」
または「Ctrlキー + S」で保存できます。
適当にファイル名を決めて保存します。
ただし、Pythonファイルの拡張子は「.py」となるので、
ここでは「hello.py」というファイル名で保存します。

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

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

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

ちゃんと「Hello World」と表示されましたね!
おめでとうございます!
これであなたも今日からPythonプログラマーです!!(^^)/
ここまで読んでくださりありがとうございます。
質問、疑問、上手くいかない等がある方はコメントにお願いします。
次回、第2回は「変数」についてです。
最近のコメント