とほほのPython入門 - 概要
目次
Pythonとは
Python は軽量なスクリプト型プログラミング言語のひとつです。
- 「パイソン」と読みます。
- python はニシキヘビを意味します。イギリスのコメディ番組「空飛ぶモンティ・パイソン」が名前の由来です。
- オランダのグイド・ヴァンロッサムによって開発されました。
- Perl, PHP, Python と合わせて「P言語」と呼ばれたりします。
- Perl, PHP, Python, Ruby, JavaScript などと合わせて「軽量プログラミング言語」と呼ばれたりもします。
- 他の言語が {...} や begin ... end でブロックを表現するのに対し、Python ではインデントの数でブロックを示します。
- Rubyと同じく、整数(int)や文字列(str)もすべてオブジェクトとして扱われます。
- Python 2 と Python 3 で若干非互換があります。
- Python 2系は python コマンド、Python 3系は python3 や python3.6 等のコマンドで提供しているケースも多いようです。
参考リンク
- Python (本家)
https://www.python.org/ - 日本Pythonユーザ会
http://www.python.jp/
インストール
Windowsの場合
http://www.python.org/ → [Downloads] → [Windows] を選び、アーキテクチャに応じたインストーラ (例えば、Python 3.7.4 の Windows x86-64 executable installer) をダウンロードしてインストールしてください。x86 は 32bit OS 用、x86-64 は 64bit OS 用です。
コマンドプロンプト
# Python 3.7.4
C:\> set Path
Path=C:\Program Files\Python37\Scripts\;C:\Program Files\Python37\...
C:\> python -V
Red Hat Enterprise Linux 7 / CentOS 7)の場合
Linux
# Python 2.7 # yum install -y python # python -V Python 2.7.5 # Python 3.6 # yum install python3 # python3 -V Python 3.6.8 2 yum update 3 yum -y install openssl-devel bzip2-devel libffi-devel 4 yum groupinstall "Development Tools" 5 wget https://www.python.org/ftp/python/3.10.7/Python-3.10.7.tgz 6 dnf -y install wget 7 wget https://www.python.org/ftp/python/3.10.7/Python-3.10.7.tgz 8 tar -xzf Python-3.10.7.tgz 9 cd Python-3.10.7 10 ./configure --enable-optimizations 11 make altinstall 12 python3.10 --version dnf -y install wget gcc make findutils wget https://www.python.org/ftp/python/3.10.7/Python-3.10.7.tgz tar zxvf ./Python-3.10.7.tgz cd ./Python-3.10.7 ./configure --enable-optimizations make make altinstall
Linux(Ubuntu / Debian)の場合
Linux
# Python 2.7
$ sudo apt-get install python
$ python -V
Linux系でソースコードからインストールする場合
CentOS 7.0 に Python 2.7.9 をインストールする例を示します。
Linux
# yum -y install wget gcc make zlib-devel gdbm-devel readline-devel # yum -y install sqlite-devel openssl-devel tk-devel bzip2-devel libffi-devel $ wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz $ tar zxvf Python-2.7.9.tgz $ cd Python-2.7.9 $ ./configure --with-threads --enable-shared --prefix=/usr/local $ make $ sudo make altinstall
Pythonの実行
Windowsの場合
環境変数 Path に python コマンドへのパスを追加します。Windows の場合は、[コントロールパネル]-[システム]-[システムの詳細設定]-[環境変数] などから、python.exe へのパス(例: C:\Python27)を追加してください。
DOSプロンプト
C:\>set Path=C:\WINDOWS\system32;C:\WINDOWS;...(略)...;C:\Python27 C:\>python -V Python 2.7.9 C:\>type hello.py print "Hello world!" C:\>python hello.py Hello world! C:\>
Linux系の場合
必要に応じて、環境変数 PATH に python コマンドへのパス(例:/usr/local/python/bin)を追加します。/usr/bin などにインストールされている場合は不要です。
Linux
$ export PATH=$PATH:/usr/local/python/bin $ python -V Python 2.7.9 $ cat hello.py print "Hello world!" $ python hello.py Hello world! $
対話モード
Python では 対話モード と呼ばれるインタラクティブ実行モードを備えています。Linux の場合は Ctrl-D、Windows の場合は Ctrl-Z Enter で対話モードを抜けることができます。
Linux
$ python3 Python 3.6.8 (default, Nov 9 2021, 14:44:26) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 5+8 13 >>> 'Hello world!' 'Hello world!' >>> a = 5 >>> b = 8 >>> a + b 13 >>> Ctrl+D
キーワード
Python 3.10 のキーワード(予約語)を下記に示します。
Python 3
2008年に Python 3.0 が登場しましたが、Python 2.x との互換性が失われています。Python 2 からの主な変更点は下記の通り。
- print命令が関数になりました。Python 2では print "..." としていましたが、print("...") と書くようになりました。Python 2でも print("...")と書くことはできますが、print命令にタプルを渡していることになるため、print("AAA", "BBB") とすると、「("AAA", "BBB")」と表示されます。
- Python 2 では、"..." がバイト列、u"..." が Unicode文字列を示していましたが、Python 3.0~3.2 では、"..." が Unicode文字列、b"..." がバイト列、u"..." はエラー(SyntaxError例外)を起こすようになりました。Python 3.3 では u"..." が復活し、"..." と同義の Unicode文字列として扱われるようになりました。
- 整数の int と long が int に統合されました。性能の差異はあるかもしれませんが、32bit や 64bit を超える長整数であっても、int で丸め誤差なく演算することができます。123L などの L 付きの整数はエラー(SyntaxError例外)となり、sys.maxint もエラー(AttributeError例外)となります。
Copyright(C) 2014-2022 杜甫々
初版:2014年12月30日、最終更新:2022年10月9日
https://www.tohoho-web.com/python/start.htm