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

Commit cb38c10

Browse files
committed
Extended logical 'and' and 'or' description and usage examples
Signed-off-by: Serhii Horodilov <sgorodil@gmail.com>
1 parent aad52bb commit cb38c10

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/basics/bool_logic.txt

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,23 @@ be ``True`` if **both** are true.
155155
| ``False`` | ``True`` | ``False`` |
156156
+---------------+---------------+-----------------------------+
157157

158-
.. todo: func_1() and func_2(); 0 and 5
158+
``and`` operator work's not only with operands of Boolean type.
159+
It's behavior:
160+
161+
- evaluate 1st operand; return it, if it's ``False`` and finish
162+
- evaluate 2nd operand; return it, if it's ``False`` and finish
163+
- return 2nd operand
164+
165+
.. code-block:: python
166+
167+
>>> 1 and 5
168+
5
169+
>>> '' and None
170+
''
171+
>>> 0 and True
172+
0
173+
>>> 5 and False
174+
False
159175

160176
Getting started with ``or`` operator
161177
------------------------------------
@@ -181,7 +197,23 @@ then the expression is ``False``.
181197
| ``False`` | ``True`` | ``True`` |
182198
+---------------+---------------+----------------------------+
183199

184-
.. todo: default mutable value, e.g. x = x or []
200+
``or`` operator work's not only with operands of Boolean type.
201+
It's behavior:
202+
203+
- evaluate 1st operand; return it, if it's ``True`` and finish
204+
- evaluate 2nd operand; return it, if it's ``True`` and finish
205+
- return 2nd operand
206+
207+
.. code-block:: python
208+
209+
>>> 42 or True
210+
42
211+
>>> None or []
212+
[]
213+
>>> 0 or True
214+
True
215+
>>> [[]] or ''
216+
[[]]
185217

186218
Comparison
187219
==========

0 commit comments

Comments
 (0)