Module 3 · Logic and bit operations · Lesson 1 of 6
Logical operators
A single comparison answers one question. Real decisions often need several — “old enough and has a ticket”. The logical operators — and, or and not — combine booleans into a single True/False.
The three logical operators
and
True and True=True
True and False=False
False and True=False
False and False=False
or
True or True=True
True or False=True
False or True=True
False or False=False
not
not True=False
not False=True
The rules are common sense. and is True only when both sides are True. or is True when at least one is. And not simply flips a value to its opposite.
Run it yourselfruns in your browser · Python
main.py
Two things worth knowing. They have a precedence, like arithmetic: not binds tightest, then and, then or — and parentheses override, as always. And Python is lazy: in A or B, if A is already True it never bothers checking B. That “short-circuiting” is handy once your conditions get expensive.