私的AI研究会 > Python覚書

Python入門

※ 最終更新:2023/07/20 

コマンドライン

python2 の場合

$ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'test'
test
>>> print('test')
test
>>> 

python3 の場合 (python2とは文法の違うものがあるのに注意)

$ source ~/env/bin/activate
(env) $ python
Python 3.7.3 (default, Dec 20 2019, 18:57:59) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'test'
  File "<stdin>", line 1
    print 'test'
               ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('test')?
>>> print('test')
test
>>> 

いずれも、終了は「Ctrl+d」 または quit() あるいは exit()

python2/python3 悩ましい違い!

 python2 からpython3 への移行はソースコードに変更を加えないといけない。変換をサポートするツールもあるようだが、最終的には手動のよう。python2 のソースコード資産も多くあるようなのでいまだに両バージョンが混在している模様。
取り敢えずの問題は「print」

python2 → print 'ABCD'  または print "ABCD"
python3 → print('ABCD') または print("ABCD")

python3 の書式は python2 でもOK のようなので、print 文は print() と表現することにしよう。

参考サイト:Python2とPython3の違い

Hello world

変数の使い方

コメント

文字コードの指定

データ型

演算方法

条件分岐(if文)

リスト(配列)

辞書(dictionary)型

タプル

ループ処理(for文)

python for文:書き方

for 変数 in オブジェクト:
    実行する処理

python for文:具体例(配列)

python for文:具体例(range関数)

python for文:具体例(_ in `オブジェクト`)

python for文:breakについて

ループ処理(while文)

 

Tips

Python には swich分がない

 

参考資料

 

Last-modified: 2023-07-20 (木) 11:49:24