コメントは#を使う。(Dartでの//)
3.1. Using Python as a Calculator
計算機として使う。四則演算など。
4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.6
Dartと特に変わりはない。
整数値はint型、小数値はfloat型。後に詳しく見ていく。
割り算( / )は常にfloat型を返す。
floor division(除算で少数部分切り捨て)は( // )演算子を使う。これはDartには無いか。あるのか?
余り部分を求める場合%を使う。確かDartでも同じ。
>>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # result * divisor + remainder 17
累乗(同じ数字を繰り返し掛け算)は( ** )演算子を使う。
>>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 128
(Dartではmathパッケージのpow()メソッドを使う。)
等号 (=
) は変数に値を代入するときに使います。代入を行っても、結果は出力されず、次の入力プロンプトが表示されます。:
>>> width = 20 >>> height = 5 * 9 >>> width * height 900
定義されていない変数を使おうとするとエラー。nullとか無いのだろうか。
>>> print(xxx) Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> print(xxx) NameError: name 'xxx' is not defined
浮動小数点を完全にサポートしています。演算対象の値(オペランド)に複数の型が入り混じっている場合、演算子は整数のオペランドを浮動小数点型に変換します:
>>> 4 * 3.75 - 1 14.0
参考
https://docs.python.org/ja/3/tutorial/introduction.html