Python summary: truth tables

For determining if something is True or False, we can use the next operators:

  • Comparison: ==, !=, <, =<, >, =>, <>
  • Logical: and, or, not, not and, not or, xor (or exclusive, represented by a caret character [^])
  • Membership: in, not in
  • Identity: is, is not

 

The truth tables is a way to view the result of the these operations. Scheme with the basic logical operators:

AND: only True if both operands is one
0 and 0   False
0 and 1   False
0 and 0   False
1 and 1   True

OR: True when any operand is one
0 and 0   False
0 and 1   True
1 and 0   True
1 and 1   True

NOT: it inverts the value of operand
not 0     True
not 1     False

XOR: True when only one of the operand is true
0 ^ 0     False
0 ^ 1     True
1 ^ 0     True
1 ^ 1     False

 

We must to know the logical operators are short-circuiting, what means if from the first operand it is possible to deduce the result, Python won’t evaluate the second operand; thereby Python saves time. For example, if in AND the first operand is False, it is not necessary to evoaluate the second one, because the result will be False.

 

Liked it? Share it! ;)

Be First to Comment

Leave a Reply