はじめに
この実験では、Python で変数がブール型 (Boolean) かどうかをチェックする方法を学びます。まず、真偽を表すブール値について理解します。ブール値はプログラミングにおける判断の基礎となります。True と False を変数に代入し、その出力を観察します。
次に、type() 関数と isinstance() 関数を使って、変数がブール値を保持しているかどうかを確認する方法を学びます。また、比較演算からブール値がどのように生成されるかを調べます。これは条件文を書いたり、プログラムの流れを制御したりする上で重要です。
ブール値を理解する
このステップでは、Python のブール値 (Boolean values) について学びます。ブール値は真偽を表し、プログラミングにおける判断の基礎となります。Python には 2 つの組み込みブール値 True と False があります。なお、True と False は Python のキーワードであり、大文字で始める必要があります。
まずは、ブール値を変数に代入して出力してみましょう。
LabEx 環境で VS Code エディタを開きます。
~/projectディレクトリにboolean_example.pyという名前の新しいファイルを作成します。~/project/boolean_example.pyboolean_example.pyファイルに以下のコードを追加します。## Assign True to a variable is_active = True ## Assign False to a variable is_admin = False ## Print the values print("Is active:", is_active) print("Is admin:", is_admin)ファイルを保存します。
ターミナルで
pythonコマンドを使ってスクリプトを実行します。python ~/project/boolean_example.py以下のような出力が表示されるはずです。
Is active: True Is admin: False
ブール値はしばしば比較演算の結果として得られます。いくつかの例を見てみましょう。
boolean_example.pyファイルを編集して、比較演算を追加します。## Comparison operations x = 10 y = 5 is_greater = x > y ## True because 10 is greater than 5 is_equal = x == y ## False because 10 is not equal to 5 print("Is x greater than y:", is_greater) print("Is x equal to y:", is_equal)ファイルを保存します。
スクリプトを再度実行します。
python ~/project/boolean_example.py以下のような出力が表示されるはずです。
Is active: True Is admin: False Is x greater than y: True Is x equal to y: False
ブール値とそれが比較からどのように生じるかを理解することは、条件文を書いたりプログラムの流れを制御したりする上で重要です。
type() を使ってブール型をチェックする
このステップでは、Python の type() 関数を使って変数のデータ型を判定する方法を学びます。具体的には、変数がブール値 (Boolean value) を保持しているかどうかをチェックします。type() 関数は、オブジェクトの型を返す組み込み関数です。
前の例を基に、定義した変数の型をチェックしてみましょう。
VS Code エディタを使って、
~/projectディレクトリのboolean_example.pyファイルを開きます。boolean_example.pyファイルを編集して、type()関数を追加します。## Assign True to a variable is_active = True ## Assign False to a variable is_admin = False ## Print the values print("Is active:", is_active) print("Is admin:", is_admin) ## Comparison operations x = 10 y = 5 is_greater = x > y ## True because 10 is greater than 5 is_equal = x == y ## False because 10 is not equal to 5 print("Is x greater than y:", is_greater) print("Is x equal to y:", is_equal) ## Check the types of the variables print("Type of is_active:", type(is_active)) print("Type of is_greater:", type(is_greater)) print("Type of x:", type(x))ファイルを保存します。
ターミナルで
pythonコマンドを使ってスクリプトを実行します。python ~/project/boolean_example.py以下のような出力が表示されるはずです。
Is active: True Is admin: False Is x greater than y: True Is x equal to y: False Type of is_active: <class 'bool'> Type of is_greater: <class 'bool'> Type of x: <class 'int'>
ご覧の通り、type() 関数はブール型の変数に対して <class 'bool'> を返し、整数型の変数に対して <class 'int'> を返します。これにより、プログラム的に変数の型をチェックし、その型に基づいて判断を下すことができます。
isinstance() で確認する
このステップでは、Python の isinstance() 関数を使って、オブジェクトが特定のクラスのインスタンスであるかどうかをチェックする方法を学びます。これは、変数がブール値を保持しているかどうかを確認する別の方法です。isinstance() 関数は 2 つの引数を取ります。チェックするオブジェクトと、比較するクラスです。オブジェクトがクラスのインスタンスであれば True を返し、そうでなければ False を返します。
isinstance() を使って、変数がブール型であるかどうかをチェックしてみましょう。
VS Code エディタを使って、
~/projectディレクトリのboolean_example.pyファイルを開きます。boolean_example.pyファイルを編集して、isinstance()関数を追加します。## Assign True to a variable is_active = True ## Assign False to a variable is_admin = False ## Print the values print("Is active:", is_active) print("Is admin:", is_admin) ## Comparison operations x = 10 y = 5 is_greater = x > y ## True because 10 is greater than 5 is_equal = x == y ## False because 10 is not equal to 5 print("Is x greater than y:", is_greater) print("Is x equal to y:", is_equal) ## Check the types of the variables print("Type of is_active:", type(is_active)) print("Type of is_greater:", type(is_greater)) print("Type of x:", type(x)) ## Check if the variables are instances of the bool class print("is_active is an instance of bool:", isinstance(is_active, bool)) print("x is an instance of bool:", isinstance(x, bool))ファイルを保存します。
ターミナルで
pythonコマンドを使ってスクリプトを実行します。python ~/project/boolean_example.py以下のような出力が表示されるはずです。
Is active: True Is admin: False Is x greater than y: True Is x equal to y: False Type of is_active: <class 'bool'> Type of is_greater: <class 'bool'> Type of x: <class 'int'> is_active is an instance of bool: True x is an instance of bool: False
ご覧の通り、isinstance(is_active, bool) は is_active がブール値であるため True を返し、isinstance(x, bool) は x が整数であるため False を返します。isinstance() 関数は、変数が特定のクラスに属しているかどうかをチェックするのに便利で、データ型を検証するより堅牢な方法を提供します。
まとめ
この実験では、Python のブール値 (Boolean values) について学びました。ブール値は真偽を表し、意思決定に不可欠です。変数に True と False を代入し、その値を出力しました。
さらに、比較演算(ある数が別の数より大きいか等しいかをチェックするなど)からブール値がどのように生成されるかを調べました。ブール値とその比較による生成元を理解することは、条件文を書き、プログラムの流れを制御するために重要です。



