|
| 1 | +.. meta:: |
| 2 | + :description: this document covers usage of logic and comparison |
| 3 | + operators in Python |
| 4 | + :author: Serhii Horodilov |
| 5 | + :keywords: python, boolean, logic, comparison, operator |
| 6 | + |
| 7 | +.. _George Boole: |
| 8 | + https://en.wikipedia.org/wiki/George_Boole |
| 9 | +.. _Boolean algebra: |
| 10 | + https://en.wikipedia.org/wiki/Boolean_algebra |
| 11 | + |
| 12 | +******************************************************************************* |
| 13 | + Boolean Logic |
| 14 | +******************************************************************************* |
| 15 | + |
| 16 | +.. image:: /../assets/img/george_boole.jpg |
| 17 | + :align: center |
| 18 | + :height: 248 |
| 19 | + |
| 20 | +`George Boole`_ put together what is now known as `Boolean algebra`_, which |
| 21 | +relies on **true** and **false** values and define a set of boolean operations: |
| 22 | +**not**, **and**, and **or**. |
| 23 | + |
| 24 | +These Boolean values and operators are helpful in programming because they help |
| 25 | +you decide the course of action in your programs. |
| 26 | + |
| 27 | +The **Python boolean** type is one of Python's built-in data types. It's used |
| 28 | +to represent the truth value of an expression. For example, the expression |
| 29 | +``1 < 2`` is ``True``, while the expression ``0 == 1`` is ``False``. |
| 30 | +Understanding how Python Boolean values behave is important to programming well |
| 31 | +in Python. |
| 32 | + |
| 33 | +Understanding Boolean |
| 34 | +===================== |
| 35 | + |
| 36 | +The Boolean type has only two possible values: |
| 37 | + |
| 38 | +- ``True`` |
| 39 | +- ``False`` |
| 40 | + |
| 41 | +No other value will have ``bool`` as it type. |
| 42 | + |
| 43 | +.. code-block:: |
| 44 | + |
| 45 | + >>> type(True) |
| 46 | + <class 'bool'> |
| 47 | + >>> type(False) |
| 48 | + <class 'bool'> |
| 49 | + |
| 50 | +The type ``bool`` is **built-in**, meaning it's always available in Python. |
| 51 | +``True`` and ``False`` are keywords in Python and can't be used as |
| 52 | +variable names. but ``bool`` type isn't and it's possible assign to the name |
| 53 | +``bool``, however this is considered bad style. |
| 54 | + |
| 55 | +Boolean as numbers |
| 56 | +------------------ |
| 57 | + |
| 58 | +Booleans are a subtype of **numeric** type in Python. This means they're |
| 59 | +numbers for all intents and purposes. In other words, you can apply arithmetic |
| 60 | +operations to Boolean, and you can also compare them to numbers. ``True`` |
| 61 | +value is treated to be 1, and ``False`` value is treated to be 0. |
| 62 | + |
| 63 | +There aren't many uses of the numerical nature of Boolean values. |
| 64 | + |
| 65 | +Truth value testing |
| 66 | +=================== |
| 67 | + |
| 68 | +Any object can be tested for truth value. By default an object considered |
| 69 | +``True``, unless its class defines either. Here are most of built-in objects |
| 70 | +considered ``False``: |
| 71 | + |
| 72 | +- constant values: ``None`` and ``False`` |
| 73 | +- zero of any numeric type: ``0``, ``0.0``, ``0j``, ``Decimal(0)``, |
| 74 | + ``Fraction(0, 1)`` |
| 75 | +- empty sequences and collections: ``''``, ``[]``, ``()``, ``{}``, ``set()``, |
| 76 | + ``range(0)``. |
| 77 | + |
| 78 | +.. hint:: |
| 79 | + |
| 80 | + If ``len(something)`` is equal to 0, than ``something`` is cast to ``bool`` |
| 81 | + as ``False``. |
| 82 | + |
| 83 | +Boolean comparison |
| 84 | +================== |
| 85 | + |
| 86 | +.. todo: not, and, or |
| 87 | + |
| 88 | +Comparison |
| 89 | +========== |
| 90 | + |
| 91 | +There are eight comparison operations in Python. They all have the same |
| 92 | +priority (which is higher than that of the Boolean operations). |
| 93 | + |
| 94 | +.. table:: Comparison operators |
| 95 | + :align: center |
| 96 | + |
| 97 | + +------------+-----------------------------------+ |
| 98 | + | Operator | Meaning | |
| 99 | + +============+===================================+ |
| 100 | + | ``<`` | strictly less than | |
| 101 | + +------------+-----------------------------------+ |
| 102 | + | ``<=`` | less than or equal | |
| 103 | + +------------+-----------------------------------+ |
| 104 | + | ``>`` | strictly greater than | |
| 105 | + +------------+-----------------------------------+ |
| 106 | + | ``>=`` | greater or equal | |
| 107 | + +------------+-----------------------------------+ |
| 108 | + | ``==`` | equal (aka *equality* comparison) | |
| 109 | + +------------+-----------------------------------+ |
| 110 | + | ``!=`` | not equal | |
| 111 | + +------------+-----------------------------------+ |
| 112 | + | ``is`` | object identity | |
| 113 | + +------------+-----------------------------------+ |
| 114 | + | ``is not`` | negated object identity | |
| 115 | + +------------+-----------------------------------+ |
0 commit comments