How to Check If a Variable Is a Boolean in Python?

I still remember the first time I spent three hours debugging a production script, only to realize a logical flag I thought was a True boolean was actually the string “True”. It’s a classic mistake that even seasoned developers make, especially when dealing with data coming from APIs or user inputs.

Python is dynamically typed, which gives us freedom, but it also means we need to be vigilant about what data our variables actually hold. Checking if a variable is a boolean is a fundamental skill that comes up constantly in data validation, configuration parsing, and control flow logic.

In this tutorial, I will walk you through the most reliable methods to check for Boolean values, straight from my daily workflow to yours.

Booleans in Python

First, let me explain what Booleans are and how they are used in Python.

Booleans are a subclass of integers in Python. The Boolean values True and False are actually instances of the bool class, which is a subclass of the int class. This means that True and False can be used in arithmetic operations, where True is equivalent to 1 and False is equivalent to 0.

Here’s a simple example to understand this:

print(True + 1)  # Output: 2
print(False + 1)  # Output: 1

Check if a Variable is a Boolean in Python

Python provides various methods to check if a variable is a boolean. Let us check one by one.

Method 1: Use the type() Function in Python

The easiest way to check if a variable is a Boolean is to use the built-in type() function. This function returns the type of the specified object.

Example

Here is an example.

def is_boolean(variable):
    return type(variable) == bool

# Test cases
print(is_boolean(True))
print(is_boolean(False))
print(is_boolean(1))
print(is_boolean("True"))

I executed the above example code and added the screenshot below.

Python check if variable is a Boolean

In this example, the is_boolean function checks if the type of the variable is bool. If it is, the function returns True; otherwise, it returns False.

Method 2: Use isinstance() in Python

Another method to check if a variable is a Boolean in Python is to use the isinstance() function. This function checks if an object is an instance or subclass of a class or a tuple of classes.

Example

Here is an example.

def is_boolean(variable):
    return isinstance(variable, bool)

# Test cases
print(is_boolean(True))
print(is_boolean(False))
print(is_boolean(1))
print(is_boolean("True"))

I executed the above example code and added the screenshot below.

check if variable is a Boolean in Python

The isinstance function is more versatile than type() because it can check for subclasses as well. However, since Booleans are a specific type, both methods are equally effective in this context.

Method 3: Use bool() in Python

The bool() function in Python is used to convert a value to a Boolean value. While this function does not directly check if a variable is a Boolean, it can be used to evaluate the truthiness of a variable.

Example

Here is an example, you can see.

def is_boolean(variable):
    return variable is True or variable is False

# Test cases
print(is_boolean(True))  # Output: True
print(is_boolean(False))  # Output: True
print(is_boolean(1))  # Output: False
print(is_boolean("True"))  # Output: False

I executed the above example code and added the screenshot below.

How to check if variable is a Boolean in Python

In this example, the is_boolean function checks if the variable is exactly True or False. This method ensures that the variable is not just truthy or falsy but specifically a Boolean value.

Method 4: Use a Tuple Comparison in Python

This method is not a direct method to check, but it will still work. You can compare the variable directly with a tuple containing True and False.

Example

def is_boolean(variable):
    return variable in (True, False)

# Examples
print(is_boolean(True))  # Output: True
print(is_boolean(False))  # Output: True
print(is_boolean(1))  # Output: False
print(is_boolean("True"))  # Output: False

This method works because it checks if the variable is exactly True or False, which are the only two Boolean values in Python.

Method 5: Use an Identity Check in Python

The final method to check if a variable is a Boolean in Python is using an identity check. You can use an identity check with the is operator.

Example

def is_boolean(variable):
    return variable is True or variable is False

# Examples
print(is_boolean(True))  # Output: True
print(is_boolean(False))  # Output: True
print(is_boolean(1))  # Output: False
print(is_boolean("False"))  # Output: False

This method checks if the variable is identical to either True or False, which effectively determines if it’s a Boolean.

In this tutorial, we have explored various methods to check if a variable is a Boolean in Python. The type() and isinstance() functions are the easiest and most widely used methods, while the bool() function can be used to evaluate the truthiness of a variable in Python.

I hope you now know how to check if a variable is a boolean in Python using the above methods.

You may also like the following related tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.