Python で変数がブール型かどうかをチェックする方法

PythonBeginner
オンラインで実践に進む

はじめに

この実験では、Python で変数がブール型 (Boolean) かどうかをチェックする方法を学びます。まず、真偽を表すブール値について理解します。ブール値はプログラミングにおける判断の基礎となります。TrueFalse を変数に代入し、その出力を観察します。

次に、type() 関数と isinstance() 関数を使って、変数がブール値を保持しているかどうかを確認する方法を学びます。また、比較演算からブール値がどのように生成されるかを調べます。これは条件文を書いたり、プログラムの流れを制御したりする上で重要です。

ブール値を理解する

このステップでは、Python のブール値 (Boolean values) について学びます。ブール値は真偽を表し、プログラミングにおける判断の基礎となります。Python には 2 つの組み込みブール値 TrueFalse があります。なお、TrueFalse は Python のキーワードであり、大文字で始める必要があります。

まずは、ブール値を変数に代入して出力してみましょう。

  1. LabEx 環境で VS Code エディタを開きます。

  2. ~/project ディレクトリに boolean_example.py という名前の新しいファイルを作成します。

    ~/project/boolean_example.py
  3. boolean_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)
  4. ファイルを保存します。

  5. ターミナルで python コマンドを使ってスクリプトを実行します。

    python ~/project/boolean_example.py

    以下のような出力が表示されるはずです。

    Is active: True
    Is admin: False

ブール値はしばしば比較演算の結果として得られます。いくつかの例を見てみましょう。

  1. 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)
  2. ファイルを保存します。

  3. スクリプトを再度実行します。

    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() 関数は、オブジェクトの型を返す組み込み関数です。

前の例を基に、定義した変数の型をチェックしてみましょう。

  1. VS Code エディタを使って、~/project ディレクトリの boolean_example.py ファイルを開きます。

  2. 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))
  3. ファイルを保存します。

  4. ターミナルで 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() を使って、変数がブール型であるかどうかをチェックしてみましょう。

  1. VS Code エディタを使って、~/project ディレクトリの boolean_example.py ファイルを開きます。

  2. 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))
  3. ファイルを保存します。

  4. ターミナルで 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) について学びました。ブール値は真偽を表し、意思決定に不可欠です。変数に TrueFalse を代入し、その値を出力しました。

さらに、比較演算(ある数が別の数より大きいか等しいかをチェックするなど)からブール値がどのように生成されるかを調べました。ブール値とその比較による生成元を理解することは、条件文を書き、プログラムの流れを制御するために重要です。