3.2. 数値¶
3.2.1. 四則演算¶
四則演算(加算・減算・乗算・除算)は下記の通りです
1 2 3 4 5 6 | test_integer = 100
print (test_integer + 10) # 加算(足し算)
print (test_integer - 10) # 減算(引き算)
print (test_integer * 10) # 乗算(掛け算)
print (test_integer / 10) # 除算(割り算)
|
出力
110
90
1000
10
3.2.3. 浮動小数点数¶
プログラミング言語では、基本的に小数点を含まない数値(int)と小数点を含む数値(float)が区別され、小数点を含む数値を浮動小数点数といいます。浮動小数点数への変換は「float」を使用します。
1 2 | test_str = '100.5'
print (float(test_str) + 100)
|
出力
200.5
また「0.5」のように整数部がゼロの場合はそれを省略して記述することができます。
1 2 3 | test_float = .5
print (test_float)
|
出力
0.5
3.2.4. 複素数¶
複素数を表すcomplexは、数値へ「j」や「J」を付与することで虚数になります。次の例は実数が「100」、虚数が「5」の複素数です。
1 2 3 4 5 | test_complex = 100 + 5j
print (test_complex)
print (test_complex.real)
print (test_complex.imag)
|
出力
(100+5j)
100.0
5.0