Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit a281e20

Browse files
committed
Added "boolean logic" document draft
Signed-off-by: Serhii Horodilov <sgorodil@gmail.com>
1 parent 155fb3c commit a281e20

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

assets/img/george_boole.jpg

136 KB
Loading

src/basics/bool_logic.txt

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
+------------+-----------------------------------+

src/basics/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
variables
1414
syntax
1515
stdtypes
16+
bool_logic
1617
controlflow
1718
functions
1819
modules

src/refs.bib

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,27 @@ @misc{realpython:methods-demystified
108108
author = "{Dan Bader}",
109109
url = {https://realpython.com/instance-class-and-static-methods-demystified/},
110110
}
111+
112+
@misc{realpython:boolean,
113+
title = "{Python Booleans: Use Truth Values in Your Code}",
114+
author = "{Moshe Zadka}",
115+
url = {https://realpython.com/python-boolean/},
116+
}
117+
118+
@misc{realpython:boolean-not,
119+
title = "{Using the \"not\" Boolean Operator in Python}",
120+
author = "{Leodanis Pozo Ramos}",
121+
url = {https://realpython.com/python-not-operator/},
122+
}
123+
124+
@misc{realpython:boolean-and,
125+
title = "{Using the \"and\" Boolean Operator in Python}",
126+
author = "{Leodanis Pozo Ramos}",
127+
url = {https://realpython.com/python-and-operator/},
128+
}
129+
130+
@misc{realpython:boolean-or,
131+
title = "{Using the \"or\" Boolean Operator in Python}",
132+
author = "{Leodanis Pozo Ramos}",
133+
url = {https://realpython.com/python-or-operator/},
134+
}

0 commit comments

Comments
 (0)