Boolean cheat sheet
with math, C, and Python notation
About
This is a list of truth tables and boolean properties expressed as math, C code, and Python code.
I made this because I wanted to teach someone to code, but all the existing cheat sheets for boolean logic were not for programming C code.
NOTE: I use "true" and "false" in the C-like column, but note that C itself does not have these keywords. Replace them with 1 and 0 as necessary!
Table of contents
NOT
C-like |
A | false | true |
!A | true | false |
Python |
A | False | True |
not A | True | False |
AND
Math |
A∧B | A=⊥ | A=⊤ |
B=⊥ | ⊥ | ⊥ |
B=⊤ | ⊥ | ⊤ |
C-like |
A && B | A==false | A==true |
B==false | false | false |
B==true | false | true |
Python |
A and B | A==False | A==True |
B==False | False | False |
B==True | False | True |
OR
Math |
A∨B | A=⊥ | A=⊤ |
B=⊥ | ⊥ | ⊤ |
B=⊤ | ⊤ | ⊤ |
C-like |
A || B | A==false | A==true |
B==false | false | true |
B==true | true | true |
Python |
A or B | A==False | A==True |
B==False | False | True |
B==True | True | True |
XOR
Math |
A⊻B | A=⊥ | A=⊤ |
B=⊥ | ⊥ | ⊤ |
B=⊤ | ⊤ | ⊥ |
XOR is the inverse of IFF (If and only if), notated by the ⇔ symbol. In other words, A⊻B ⇔ ¬(A⇔B).
C-like |
* | A==false | A==true |
B==false | false | true |
B==true | true | false |
*There is no logical xor operator in C. Use A ^ B if you are sure that A and B are either 0 or 1; otherwise use !A != !B.
Python |
* | A==False | A==True |
B==False | False | True |
B==True | True | False |
*There is no logical xor operator in Python. Use A ^ B if you are sure that A and B are of type bool; otherwise use bool(A) != bool(B).
IMPLY (IF)
Math |
A⇒B | A=⊥ | A=⊤ |
B=⊥ | ⊤ | ⊥ |
B=⊤ | ⊤ | ⊤ |
*This is commonly used in boolean logic, but not commonly used in C code.
*This is commonly used in boolean logic, but not commonly used in Python code.
Name | Math | C-like | Python |
identity for OR | ⊥∨A=A | false || A == A | False or A == A |
annihilator for AND | ⊥∧A=⊥ | false && A == false | False and A == False |
annihilator for OR | ⊤∨A=⊤ | true || A == true | True or A == True |
identity for AND | ⊤∧A=A | true && A == A | True and A == A |
idempotence | A∨A=A | A || A == A | A or A == A |
idempotence | A∧A=A | A && A == A | A and A == A |
double negative | ¬(¬A)=A | !!A == A | not not A == A |
NOTE: Using the annihilator functions, something called "lazy evaluation" will skip evaluating the later parts of an expression if it's clear what the answer will be. For example, if your condition is "(username is not blank) AND (password is not blank) AND (username and password match)", you can skip checking if the password is not blank or if the username and password match if the username is blank.
Name | Math | C-like | Python |
Commutative | A∨B=B∨A | A || B == B || A | A or B == B or A/td> |
Commutative | A∧B=B∧A | A && B == B || A | A and B == B and A |
Associative | A∨(B∨C)=(A∨B)∨C | A || (B || C) == (A || B) || C | A or (B or C) == (A or B) or C |
Associative | A∧(B∧C)=(A∧B)∧C | A && (B && C) == (A && B) && C | A and (B and C) == (A and B) and C |
Distributive | A∧(B∨C)=(A∧B)∨(A∧C) | A && (B || C) == (A && B) || (A && C) | A and (B or C) == (A and B) or (A and C) |
Distributive | A∨(B∧C)=(A∨B)∧(A∨C) | A || (B && C) == (A || B) && (A || C) | A or (B and C) == (A or B) and (A or C) |
NOTE: These are only "equal" insofar as the final result is equal. Due to lazy evaluation, some ways to express the same condition in code are more efficient than others!
Name | Math | C-like | Python |
negation of disjunction | ¬(A∨B) = ¬A∧¬B | !(A || B) == !A && !B | not(A or B) == not A and not B |
negation of conjunction | ¬(A∧B) = ¬A∨¬B | !(A && B) == !A || !B | not(A and B) == not A or not B |