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

Commit e8d8c4c

Browse files
committed
Added 'logical negation' (not) description
Signed-off-by: Serhii Horodilov <sgorodil@gmail.com>
1 parent a281e20 commit e8d8c4c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/basics/bool_logic.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,53 @@ Boolean comparison
8585

8686
.. todo: not, and, or
8787

88+
Python provides 3 logical operators:
89+
90+
.. table::
91+
:align: center
92+
93+
+----------+----------------+
94+
| Operator | Logic operator |
95+
+==========+================+
96+
| ``not`` | Negation |
97+
+----------+----------------+
98+
| ``and`` | Conjunction |
99+
+----------+----------------+
100+
| ``or`` | Disjunction |
101+
+----------+----------------+
102+
103+
With these operators, you can build expressions by connecting Boolean
104+
expressions with each other. These operators are **keywords** of the language,
105+
so you cannot use them as identifiers without causing a *SyntaxError*.
106+
107+
Getting started with Python's ``not`` operator
108+
----------------------------------------------
109+
110+
The ``not`` operator is the Boolean or logic operator that implements negation
111+
in Python. It's **unary**, which means that it takes only one **operand**.
112+
The operand can be a Boolean expression or any Python object. The task of
113+
``not`` is to reverse the truth value of its operand.
114+
115+
.. table:: not operator
116+
:align: center
117+
118+
+-----------+-------------+
119+
| operand | not operand |
120+
+===========+=============+
121+
| ``True`` | ``False`` |
122+
+-----------+-------------+
123+
| ``False`` | ``True`` |
124+
+-----------+-------------+
125+
126+
This functionality makes it worthwhile in several situations:
127+
128+
129+
- Checking **unmet conditions** in the context of ``if`` statements and
130+
``while`` loops
131+
- **Inverting the truth value** of an object or expression
132+
- Checking if a **value is not in a given container**
133+
- Checking for an **object's identity**
134+
88135
Comparison
89136
==========
90137

0 commit comments

Comments
 (0)