3. 形式ばらない Python の紹介 (数)
Python チュートリアル — Python 3.6.13 ドキュメント
3. 形式ばらない Python の紹介 3.1. Python を電卓として使う 3.1.1. 数 3.1.2. 文字列型 (string) 3.1.3. リスト型 (list) 3.2. プログラミングへの第一歩
- 数
- インタプリタを起動して、簡単な電卓の代わりに使う
> python >>> >>> 2+2 4 >>> 50-5*6 20 >>> (50-5*6)/4 5.0 >>> 8/5 1.6 >>>
- 2種類の除算の演算子( / , // )の違い。答えの型の確認。
>>> type(17/3) <class 'float'> >>> type(17//3) <class 'int'> >>>
- べき乗は「**」
>>> 2**2 4 >>> 2**3 8 >>> 2**16 65536 >>>
- 変数を利用した演算。未定義の変数を使用するとエラー
>>> 2**16 65536 >>> n=16 >>> n/8 2.0 >>> m/8 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'm' is not defined >>>
- 対話モードの場合は、最後の演算結果は「 _ (アンダースコア) 」に格納される
>>> 2**3 8 >>> _ 8 >>>
- スクリプトだと、直前の演算結果が「_(アンダースコア)」に格納されない…。
# test_.py m =16 n = 8 print(m/n) print(_/n)
> python .\test_.py 2.0 Traceback (most recent call last): File ".\test_20181013.py", line 4, in <module> print(_/n) NameError: name '_' is not defined