How to Check Variable Type is Boolean in Python?

Someone from the New York Python user group asked me how to check if the variable type is boolean in Python. There are a few methods to check it easily. In this tutorial, let me show you how with different examples, starting with a basic one and ending with an advanced one.

To check if a variable is Boolean in Python, the most straightforward and recommended method is to use the isinstance() function. This function checks if an object is an instance of a specified type. For example, isinstance(var, bool) will return True if var is a Boolean (True or False), and False otherwise. This method is both simple and efficient for determining the type of a variable.

Check Variable Type is Boolean in Python

A Boolean in Python is a data type that can hold one of two values: True or False. These values are used in conditional statements to control the flow of a program. For example, you might check if a user is logged in or if a certain condition is met before executing a block of code.

Method 1: Using isinstance()

The most used way to check if a variable is a Boolean in Python is by using the isinstance() function. It returns True if the variable is of the specified type, and False otherwise.

Let me show you different examples, starting from a basic example:

Example-1:

Here is a basic example.

x = True
print(isinstance(x, bool))  # Output: True

Example-2:

Let me show you how to create a function and check if the variable type is boolean. Here is the complete code.

# Define a variable
is_sunny = True

# Function to check if the variable is a boolean
def is_boolean(value):
    return isinstance(value, bool)

# Check if the variable is a boolean
if is_boolean(is_sunny):
    print(f"The variable 'is_sunny' is a boolean: {is_sunny}")
else:
    print(f"The variable 'is_sunny' is not a boolean.")

In this example, is_sunny is a Boolean variable. The is_boolean function uses isinstance() to check if is_sunny is a Boolean and prints the appropriate message.

I executed the above Python code, and you can see the output in the screenshot below:

python check variable type is boolean

Example-3:

Now, let me give you an advanced example.

Let’s say you are developing a system to track whether certain states in the USA have passed a specific law.

# Define a dictionary with states and their law status
states_law_status = {
    'California': True,
    'Texas': False,
    'New York': True,
    'Florida': False
}

# Function to check if the status is a boolean
def is_boolean(status):
    return isinstance(status, bool)

# Check each state's law status
for state, status in states_law_status.items():
    if is_boolean(status):
        print(f"The law status for {state} is a boolean: {status}")
    else:
        print(f"The law status for {state} is not a boolean.")

In this example, the is_boolean function uses isinstance() to check if the status is a Boolean. This is useful for ensuring the data type is correct before performing further operations.

Check out Check the Type of a Variable in Python

Method 2: Using Type Comparison

Another method in Python to check if a variable is a Boolean is by directly comparing its type using the type() function.

Syntax

type(variable) == bool

Here, I will also show you three different examples to help you understand better.

Example-1:

Here is a basic example

var = False
print(type(var) == bool)  # Output: True

Example-2:

Here is another example where I created a function that you can reuse.

# Define a variable
is_raining = False

# Function to check if the variable is a boolean
def is_boolean(value):
    return type(value) is bool

# Check if the variable is a boolean
if is_boolean(is_raining):
    print(f"The variable 'is_raining' is a boolean: {is_raining}")
else:
    print(f"The variable 'is_raining' is not a boolean.")

Here, is_raining is a Boolean variable. The is_boolean function checks if the type of is_raining is bool and prints the appropriate message.

You can see the output in the screenshot below:

check variable type is Boolean in Python

Example-3:

Now, let me give you an advanced example.

Consider you are working on a voting system and need to check if a vote is valid (either True or False).

# List of votes
votes = [True, False, 'yes', 'no', True, False]

# Function to check if the vote is a boolean
def is_boolean(vote):
    return type(vote) is bool

# Check each vote
for vote in votes:
    if is_boolean(vote):
        print(f"The vote {vote} is valid.")
    else:
        print(f"The vote {vote} is invalid.")

Here, the is_boolean function checks if the type of vote is bool. This method is straightforward but less flexible than isinstance() because it does not support checking against multiple types at once.

Read Check if a Variable is None in Python

Method 3: Using bool() Function

While bool() is typically used to convert a value to a Boolean, it can also be used in a roundabout way to check if a variable is Boolean.

Syntax

bool(variable)

Let me show you a few examples.

Example-1:

Here is a basic example

var = 1
print(bool(var))  # Output: True

Method 4: Using type() and ==

In this method, I will show you how to use the type() and the equality operator (==) to compare the type directly in Python.

Example-1:

Let me first show you a simple example.

# Define a variable
my_var = True

# Check if the variable is a boolean using type() and ==
is_bool = type(my_var) == bool

# Print the result
print(is_bool)  # Output: True

In this example, my_var is a Boolean variable. The type() function is used to get the type of my_var, which is then compared to bool. The result is stored in is_bool, which is then printed.

Example-2:

Here is an advanced example.

# Define a variable
is_weekend = True

# Function to check if the variable is a boolean
def is_boolean(value):
    return type(value) == bool

# Check if the variable is a boolean
if is_boolean(is_weekend):
    print(f"The variable 'is_weekend' is a boolean: {is_weekend}")
else:
    print(f"The variable 'is_weekend' is not a boolean.")

In this example, is_weekend is a Boolean variable. The is_boolean function uses type() and == to check if is_weekend is a Boolean and prints the appropriate message.

You can see the output in the screenshot below:

How to check variable type is Boolean in Python

I hope you are now learning how to check the type of variable is a boolean in Python. All these methods and examples must help us understand you better.

You may also like:

Leave a Comment