Python Practice › If-Else Programs › Program 45
Check a boolean value
Write a Python program to check whether a value is of boolean type.
Input
True, 1
Output
True is a boolean 1 is not a boolean
Approach
- Use
isinstance(value, bool); note1is an int, not a bool.
Solution
for value in [True, 1]: if isinstance(value, bool): print(value, "is a boolean") else: print(value, "is not a boolean")Output
True is a boolean 1 is not a boolean
Line by line
Line 2
True == 1 is True, but 1 is still an int, so the type check distinguishes them.
Learn the concept
New to this topic? Read the lesson, then come back.