Data Type - 'bool' for Boolean Values

This section describes the 'bool' data type, which only has two objects, the True and False objects.

What Is the "bool" Data Type? "bool" is the Boolean data type in Python. It only has two objects, the True and False objects, with the following features:

1. The Boolean objects can be referred by the keywords, True and False, in Python code.

2. The Boolean objects, True and False, are actually mapped to numeric values when used in comparison operations with int, float and complex data types.

bool    int   float   complex
-----   ---   -----   -------
True      1     1.0      1+0j
False     0     0.0      0+0j

Here are some sample code of using Boolean objects in comparison operations:

>>> True == 1
True
>>> False == 0
True
>>> False < 1
True
>>> False < 2
True
>>> True < 2
True
>>> True < -2
False

>>> True == 1.0
True
>>> True < 2.0
True
>>> True < 0.5
False

>>> True == 1+0j
True

3. When Boolean objects are used in the equality operation, ==, with any other non-numeric data type, the result is False. Similarly, the result is True, when used in non-equality operation, !=, with any other non-numeric data type.

>>> True == '1'
False
>>> True == 'True'
False

>>> True != 'True'
True
>>> False != 'True'
True
>>> False != None
True
>>> False == None
False

4. You use the bool(object) function to cast any objects of any type into a Boolean object. Here is the casting logic for an object represented by "obj":

Here are some example of using the bool(object) function.

>>> bool([])
False
>>> bool([0])
True

>>> bool(None)
False

>>> bool(10)
True
>>> bool(0.0)
False

>>> bool(0j)
False
>>> bool(10+0j)
True

# "bool" is the variable name referring to the bool() function object
>>> bool(bool)
True

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

Built-in Data Types

 Introduction to Data Type

 Common Features of All Data Types

 Data Type - NoneType for Nothing

Data Type - 'bool' for Boolean Values

 Data Type - 'int' for Integer Values

 Data Type - 'float' for Real Numbers

 Data Type - 'bytes' for Byte Sequence

 Data Type - 'str' for Character String

 Data Type - 'tuple' for Immutable List

 Data Type - 'list' for Mutable List

 Data Type - 'set' for Unordered Collection

 Data Type - 'dict' for Dictionary Table

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators and Generators

 List, Set and Dictionary Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB