You can use the bool() constructor to convert an integer value to a boolean value in Python. The bool() accepts an integer and returns either True or False.
If your input integer is 0, bool() returns False. For any integer other than zero, it will return True. For example, if you want to convert 1 to a boolean, bool(1) returns True.
print(bool(0)) # Output: False print(type(bool(0))) # Output: <class 'bool'> print(bool(0.5)) # Output: True print(type(bool(0.5))) # Output: <class 'bool'> print(bool(1)) # Output: True print(type(bool(1))) # Output: <class 'bool'> print(bool(100)) # Output: True print(type(bool(100))) # Output: <class 'bool'>
One thing to note here is that a non-zero integer (e.g., 2) is truthy but not equal to True.
print(2 == True) # Output: False (True is 1) print(bool(2)) # Output: True
Boolean type is a subclass of integers.
print(isinstance(True, int)) # Output: True print(True + True) # Output: 2 (since True == 1)
That means True and False are subclasses of int.
In libraries like NumPy, bool(5) behaves like Python. However, array-wide operations may differ.
Negative integers
If you convert a negative integer into a boolean, it will still return True.
print(bool(-21)) # Output: True print(bool(-1.9)) # Output: True
Implicit Conversion in Boolean Contexts
Condition statements like “if”, Python automatically evaluates integers in conditions using truthiness.
data = 5 if data: print("Non-zero") # Executes because "data" is truthy # Output: Non-zero y = 0 if not y: print("Zero") # Executes because y is falsy # Output: Zero
Checking for Specific Values
You can use truthy/falsy logic directly inside the if statement instead of checks like if x != 0 or if x == 1.
# Redundant if x != 0: pass # Better if x: pass
Inverting Boolean Values
Python provides a “not” operator that you can use to invert the boolean representation:
print(not 0) # Output: True (0 is falsy) print(not 5) # Output: False (5 is truthy)
Conclusion
For explicit conversion, use the bool() constructor to transform from an integer to a boolean. While working with condition statements (if or while), Python automatically infers 0 to False and other integers to True.