Boolean logic is a fundamental skill for any programmer. You need to have a solid understanding to be able to grasp the meaning of code quickly—and write your own bug-free code.
Oftentimes, you’ll find that the source of bugs in your code is because of flaws in your logic. So improving your ability to reason logically will improve your overall coding skill and will save you hours of painful debugging.
Boolean logic is the science of formulating and combining logical statements.
Definition: A logical statement is a statement that evaluates to a Boolean value
True
orFalse
.
You can create logical statements recursively using the four Python operators and
, or
, not
, and the bracket operator ( )
.
The simplest logical statement would be A=True
or A=False
. Now, you can create more complex logical statements using the Python operators:
(A)
. The logical statement(A)
evaluates toTrue
if the statementA
is alreadyTrue
. Usually, you’ll use the bracket operator to reorder the precedence relationship. This is called the bracket (or parenthesis) operator.not A
. The logical statementnot A
evaluates toTrue
if statementA
evaluates toFalse
. This is called the negation operator.A and B
. The logical statementA and B
evaluates toTrue
if both statementsA
andB
are alreadyTrue
. If only one of them isFalse
, the statementA and B
evaluates toFalse
, too. This is called the logical AND operator.A or B
. The logical statementA or B
evaluates toTrue
if at least one statementA
orB
is alreadyTrue
. Only if both of them areFalse
, the statementA or B
evaluates toFalse
. This is called the logical OR operator.
Here’s a number of basic examples in Python code:
a = True b = False print(a) # True print(b) # False print(not a) # False print(not b) # True print(a and b) # False print(a and a) # True print(b and b) # False print(a or b) # True print(a or a) # True print(b or b) # False print((a and b) or b) # False
Here’s a number of advanced examples in Python code:
# Example 0 a, b, c, d = True, False, False, True if not a or not c: print('yes') else: print('python') ''' yes ''' # Example 1 a, b, c, d = False, True, False, False if not d and b and d: if not a and not b: print('yes') elif a and c: print('yes') print('yes') elif b: if d: print('love') print('python') else: print('python') ''' python ''' # Example 2 a, b, c, d = False, True, True, True out = any([ c or b and not d, a and b or c or not b, b and d and a or c, d and not d or b or a, not c, not b or b, a, ]) print(out) ''' True ''' # Example 3 a, b = False, True out = (a and b and not a) or (not b) or (b and a) or (a and not a and not b) print(out) ''' False ''' # Example 4 a, b, c = True, True, True if b or not a or a: print('love') else: print('python') ''' love ''' # Example 5 a, b = True, False out = (a and b) or (a and b and not a) or (a) print(out) ''' True ''' # Example 6 a, b, c = True, False, True if c or a or not b or not a: print('42') else: print('python') ''' 42 ''' # Example 7 a, b, c = True, False, False out = any([ a, c and not b, c or a, b or not b, c or a, b or c or a, ]) print(out) ''' True ''' # Example 8 a, b, c = True, True, True if not c and b and c: print('yes') else: print('python') ''' python ''' # Example 9 a, b = False, True out = (not a and b and a) or (b and not b and a) or (b) or (b) print(out) ''' True ''' # Example 10 a, b, c, d = False, True, True, False if not b: print('love') else: print('42') ''' 42 '''
Where to go from here?
Python is a profitable career path nowadays. But in order to thrive as a Python coder, you need to understand the basics very well. Especially, you need to “see” the meaning of source code very quickly.
To this end, I wrote the “Coffee Break Python” book series:
Check them out!