How to Check If a Variable Is a Boolean in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a variable is a Boolean in Python. You'll begin by understanding Boolean values, which represent truth or falsehood and are fundamental for decision-making in programming. You'll assign True and False to variables and observe the output.

The lab then guides you through using the type() function and the isinstance() function to confirm whether a variable holds a Boolean value. You'll also explore how Boolean values arise from comparison operations, which is crucial for writing conditional statements and controlling program flow.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/BasicConceptsGroup -.-> python/booleans("Booleans") python/BasicConceptsGroup -.-> python/type_conversion("Type Conversion") python/ControlFlowGroup -.-> python/conditional_statements("Conditional Statements") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") subgraph Lab Skills python/booleans -.-> lab-559598{{"How to Check If a Variable Is a Boolean in Python"}} python/type_conversion -.-> lab-559598{{"How to Check If a Variable Is a Boolean in Python"}} python/conditional_statements -.-> lab-559598{{"How to Check If a Variable Is a Boolean in Python"}} python/build_in_functions -.-> lab-559598{{"How to Check If a Variable Is a Boolean in Python"}} end

Understand Boolean Values

In this step, you will learn about Boolean values in Python. Boolean values represent truth or falsehood and are fundamental to decision-making in programming. Python has two built-in Boolean values: True and False. Note that True and False are keywords in Python and must be capitalized.

Let's start by assigning Boolean values to variables and printing them:

  1. Open the VS Code editor in the LabEx environment.

  2. Create a new file named boolean_example.py in the ~/project directory.

    ~/project/boolean_example.py
  3. Add the following code to the boolean_example.py file:

    ## 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. Save the file.

  5. Run the script using the python command in the terminal:

    python ~/project/boolean_example.py

    You should see the following output:

    Is active: True
    Is admin: False

Boolean values are often the result of comparison operations. Let's see some examples:

  1. Modify the boolean_example.py file to include comparison operations:

    ## 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. Save the file.

  3. Run the script again:

    python ~/project/boolean_example.py

    You should see the following output:

    Is active: True
    Is admin: False
    Is x greater than y: True
    Is x equal to y: False

Understanding Boolean values and how they arise from comparisons is crucial for writing conditional statements and controlling the flow of your programs.

Use type() to Check for Boolean

In this step, you will learn how to use the type() function in Python to determine the data type of a variable, specifically checking if a variable holds a Boolean value. The type() function is a built-in function that returns the type of an object.

Building upon the previous example, let's check the types of the variables we defined:

  1. Open the boolean_example.py file in the ~/project directory using the VS Code editor.

  2. Modify the boolean_example.py file to include the type() function:

    ## 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. Save the file.

  4. Run the script using the python command in the terminal:

    python ~/project/boolean_example.py

    You should see the following output:

    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'>

As you can see, the type() function returns <class 'bool'> for Boolean variables and <class 'int'> for integer variables. This allows you to programmatically check the type of a variable and make decisions based on its type.

Confirm with isinstance()

In this step, you will learn how to use the isinstance() function in Python to check if an object is an instance of a particular class. This is another way to confirm if a variable holds a Boolean value. The isinstance() function takes two arguments: the object to check and the class to check against. It returns True if the object is an instance of the class, and False otherwise.

Let's use isinstance() to check if our variables are Booleans:

  1. Open the boolean_example.py file in the ~/project directory using the VS Code editor.

  2. Modify the boolean_example.py file to include the isinstance() function:

    ## 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. Save the file.

  4. Run the script using the python command in the terminal:

    python ~/project/boolean_example.py

    You should see the following output:

    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

As you can see, isinstance(is_active, bool) returns True because is_active is a Boolean value, while isinstance(x, bool) returns False because x is an integer. The isinstance() function is useful for checking if a variable belongs to a specific class, providing a more robust way to verify data types.

Summary

In this lab, you learned about Boolean values in Python, which represent truth or falsehood and are essential for decision-making. You assigned True and False to variables and printed their values.

Furthermore, you explored how Boolean values result from comparison operations, such as checking if one number is greater than or equal to another. Understanding Boolean values and their origins from comparisons is crucial for writing conditional statements and controlling program flow.