Hi there. This page is a short guide to boolean variables and truth tables in Python.
Reference: Starting Out With Python – Second Edition By Tony Gaddis
A boolean variable is a variable that can either take on True or False. Boolean variables can be viewed as signals or flags which can help with if statements.
if test_score > 50
passed = True
if passed == True:
print("You have passed!")
Instead of passed == True:, you may see just if passed: which means the same thing.
Logical Operators & Truth Tables
Before getting into truth tables, it is important to to introduce the logical operators and, or.
This or operator connects two conditions together into one. For the or statement to be true, one of the two or both of them have to be true.
- True or True = True
- True or False = True
- False or True = True
- False or False = False
With the and operator, both statements have to be true in for the and compound expression to be true.
- True and True = True
- True and False = False
- False and True = False
- False and False = False
Like in the English language, not means the opposite. There are no gray areas (half-truths, half lies and the like).
- not True = False
- not False = True