0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WindowsによるPython入門 #04: 数値

Last updated at Posted at 2023-02-18

はじめに

今回は色々な数値に関する型として、ブール型、整数型、浮動小数点数型の 3 つを解説します。

YouTube動画

WindowsによるPython入門 (第四回) 数値

ブール型

ブール型とは、論理型や真偽値型とも呼ばれ、「True」もしくは「False」のどちらかの値の型です。
True が真で、False が偽を意味します。

インタラクティブシェルで確認します。
ブール値として、True や False という値を使用出来ます。
type() 関数で型を調べると、bool 型である事が分かります。

>>> True
True
>>> False
False
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>

真と偽は、式が正しい場合が「真」で、正しくない場合が「偽」です。

>>> 1 < 10   # 式が正しい
True
>>> 99 < 10  # 式が正しくない
False

また、bool 型のコンストラクタを使うと bool 値に変換出来ます。
(コンストラクタは、あとでクラスを解説する時に再度解説します。ここでは、関数だと思って頂いて構いません)
「整数の 0」や「空の文字列」などが False になります。他にも False になるケースがありますが、基本的には、「0 の値」や「空の値」の場合に False になります。

>>> bool(True)
True
>>> bool(False)  # False は偽
False
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(0)  # 整数の 0 は偽
False
>>> bool("abc")
True
>>> bool("")  # 空文字は偽
False
>>> bool(None)  # None は偽
False

ちなみに、Python の予約語を調べると、True と False が入っています。

>>> help("keywords")

Here is a list of the Python keywords.  Enter any keyword to get more help.

False               class               from                or
None                continue            global              pass
True                def                 if                  raise
and                 del                 import              return
as                  elif                in                  try
assert              else                is                  while
async               except              lambda              with
await               finally             nonlocal            yield
break               for                 not

ですので、前回解説した通り、True や False は、変数名として使用出来ません。

>>> True = 1
  File "<stdin>", line 1
    True = 1
    ^^^^
SyntaxError: cannot assign to True

整数型

整数型は、整数値を表す型です。
具体的に確認します。以下、は全て整数型です。

>>> type(999)
<class 'int'>
>>> type(-1)
<class 'int'>
>>> type(0)
<class 'int'>

数字のみを書くと、10進数になりますが、数字の先頭に「0b」、「0o」、「0x」を付けると、それぞれ、2進数、8進数、16進数を意味します。

>>> 0b10
2
>>> 0o10
8
>>> 0x10
16

逆の変換は、それぞれ、bin()、oct()、hex() 関数を使用します。

>>> bin(2)
'0b10'
>>> oct(8)
'0o10'
>>> hex(16)
'0x10'

また見やすいように、アンダースコアを付ける事は可能です。

>>> 1_000_000
1000000

int のコンストラクタを使うと、整数値に変換出来ます。
また、基数を指定して、2進数、8進数、16進数を変換出来ます。

>>> int(True)
1
>>> int(False)
0
>>> int(9.9)
9
>>> int("10")  # 文字列を整数値に変換
10
>>> int("10", 2)   # 2進数
2
>>> int("10", 8)   # 8進数
8
>>> int("10", 16)  # 16進数
16

Python では、「**」でべき乗を表します。
int 型は上限がなく、メモリの許す限り、いくらでも大きい数を表す事が可能です。

>>> 2 ** 1000  # 2の1000乗
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

浮動小数点数型

浮動小数点数型は、実数値を表す型です。
具体的に確認します。以下、は全て浮動小数点数型です。

>>> type(1.0)
<class 'float'>
>>> type(1e10)
<class 'float'>
>>> type(-0.01)
<class 'float'>

float のコンストラクタを使うと、浮動小数点数値に変換出来ます。
特殊な値として、「inf」 (無限大)、「NaN」(非数)も扱えます。
(NaNは、「Not a Number」の略で、不正な計算によって生じた値である事を意味します)

>>> float(True)
1.0
>>> float(1)
1.0
>>> float('123')
123.0
>>> float('-1.0')
-1.0
>>> float('inf')  # 無限大
inf
>>> float('nan')  # 非数
nan

float の最大値・最小値は、以下の通りです。
(これは、C 言語の double 型の範囲と同じです)

>>> import sys
>>> sys.float_info.max  # 最大値
1.7976931348623157e+308
>>> sys.float_info.min  # 最小値
2.2250738585072014e-308

数値の演算

以下の演算子が使用出来ます。

  • 「+」 (足し算)
  • 「-」 (引き算、マイナス)
  • 「*」 (掛け算)
  • 「/」 (割り算)
  • 「//」 (割り算の商の整数部分)
  • 「%」 (割り算の余り)
  • 「**」 (べき乗)

演算子を使用して、以下のように演算出来ます。
また、整数値と浮動小数点数値を足すと、結果は浮動小数点数値になります。
ゼロで割るとエラーになります。

>>> 1 + 2
3
>>> 1 + 2 * 3    # かけ算が足し算より優先
7
>>> (1 + 2) * 3  # カッコの計算が優先
9
>>> 1 - 2
-1
>>> 10 / 3
3.3333333333333335
>>> 10 // 3  # 割り算の商の整数部分。 int(10 / 3) と同じ意味
3
>>> 10 % 3   # 割り算の余り
1
>>> 2 ** 10  # 2の10乗
1024
>>> 1 + 2.0  # float(1) + 2.0 と同じ意味
3.0
>>> 1 / 0  # ゼロで割るとエラー
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

おわりに

以上、数値に関する型のまとめでした。
次回は、文字列型に関して解説します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?