Check a boolean value

EasyIf-Else Programs · 45 of 64

Write a Python program to check whether a value is of boolean type.

Advertisement
Input
True, 1
Output
True is a boolean
1 is not a boolean

Approach

  1. Use isinstance(value, bool); note 1 is an int, not a bool.

Solution

Python
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.

More if-else programs

← All 64 If-Else Programs

Leave a Comment