Boolean Data Object

Boolean is named after Mathematician George Boole.

The Boolean data type allow us to implement logic in our code by simplifying statements to be either True or False.

There are two Boolean data-typed possible values in Python.

  • True

  • False

Usage:

Truthy and Falsy Values in Python 3

In programming (Python), certain values can be also True or False in Conditional Situations. We call these Truthy or Falsy values.

The rules are as follows:

  • Idea of Emptiness is False β†’ Keyword: None is False

  • Therefore, 0, '', "" , [] are all empty β†’ False

  • 1 represents β€œON” in binary β†’ True; 0 β†’ False

  • Lastly, ANY NON-FALSY values are True

Type Conversion Function: bool()

The built-in function to convert one data value to Boolean is: bool()

# Example

print('bool(\'\'):', bool('')) # Empty String
print('bool("Hello"):', bool("Hello")) # Non-Empty String
print('bool([]):', bool([]))
print('bool(1):', bool(1))
print('bool(0):', bool(0))

'''
bool(''): False
bool("Hello"): True
bool([]): False
bool(1): True
bool(0): False
'''

Last updated