How to Declare a Boolean Value in Python

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.

  1. False
  2. None
  3. 0 (integer)
  4. 0.0 (float)
  5. "" (empty string)
  6. () (empty tuple)
  7. [] (empty list)
  8. {} (empty dictionary).

You can explicitly use the bool() function to convert a value to a boolean.

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.