Loading...
Loading...
00:00:00

Python Boolean Data Type

In Python, the boolean data type is used to represent truth values, which are either True or False. Boolean values are used in control flow statements such as if-else statements to make decisions based on whether a condition is True or False.

You can define a boolean variable by assigning either True or False to it:

a = True
b = False

You can also use comparison and logical operators to evaluate expressions and produce boolean values:

x = 10
y = 20
print(x < y)  # True
print(x == y)  # False
print(not (x < y))  # False

The and and or operators allow you to combine multiple boolean values:

print(True and False)  # False
print(True or False)  # True

It's also possible to convert other data types to boolean using the bool() function. The bool() function is used to convert other data types to a Boolean value. It returns True for values that evaluate to True and False for values that evaluate to False. For example:

print(bool(0))  # False
print(bool(10))  # True
print(bool("Hello"))  # True
print(bool(""))  # False

Keep in mind that the bool() function considers the following values to be False:

  • None
  • False
  • 0 (integer)
  • 0.0 (floating-point number)
  • [] (empty list)
  • () (empty tuple)
  • {} (empty dictionary)
  • "" (empty string)

All other values are considered to be True.