Python has many primitive data types, and “bool” is one of them. In this tutorial, we will see what a boolean value is and how to declare a boolean value.
Python boolean
The boolean data type can have only two values: True and False.
The primary use of boolean values in Python is to check if the statement is Truthy or Falsy values.
How to declare a boolean value in Python
To declare a boolean value in Python, assign a True or False value to a variable. For example, variable a = True or a = False. The variable a becomes a boolean now.
You can represent boolean values as 0 and 1, which is a much better way to store them as integers in real-world applications.
Code implementation
data = True
info = False
ai = 1
human = 0
print(data)
print(type(data))
print(info)
print(type(info))
print(ai)
print(type(ai))
print(human)
print(type(human))
Output
True
<class 'bool'>
False
<class 'bool'>
1
<class 'int'>
0
<class 'int'>
You can see that type() function returns the data type of a variable.
For values like True or False, it returns class “bool”; for values like 0 and 1, it returns class “int”.
Python bool() function
The bool() is a built-in Python function that converts any value to a boolean (True or False) value.
The following values in Python are considered False.
False
None
0
(integer)0.0
(float)""
(empty string)()
(empty tuple)[]
(empty list){}
(empty dictionary).
You can explicitly use the bool() function to convert a value to a boolean.
That’s it for this tutorial.