DEV Community

Cover image for Mathematical Notation for Python Developers | Propositional Logic
Adérito Xavier
Adérito Xavier

Posted on

Mathematical Notation for Python Developers | Propositional Logic

Introduction

As developers, we require to be always up to date with the new tech stack however we tend to forget the importance of the underlying structure in which new tech stacks are built-in.

I am not saying we are required to be ninjas at assembly or binary but a good understanding of how they work is necessary in order to progress as a developer.

Learning Propositional Logic can help you understand how circuits work programming conditions, writing your code with mathematical notation amongst other things.

Alt Text

Who Is This Article For

By now, you might be wondering: “Do I need to know Python or Binary to understand this article?”. You do not!

I made this article for anyone who is interested in learning mathematical notation.

AND | Conjunction

AND expressions are denoted with the symbol .

a ∧ b = a and b

Both a and b must be true in order for an AND statement to be true or else it will always be false.

|1 ∧ 0 = 0|0 ∧ 1 = 0|1 ∧ 1 = 1 |0 ∧ 0 = 0|


x = (1 and 0)
print(x)
# >> 0
x = (1 and 1)
print(x)
# >> 1
x = (False and True)
print(x)
# >> False
x = (True and True)
print(x)
# >> True

OR | Disjunction

OR expressions are denoted with the symbol .

a ∨ b = a or b

Either a or b must be true in order for an OR statement to be true.

|1 ∨ 0 = 1|0 ∨ 1 = 1|1 ∨ 1 = 1|0 ∨ 0 = 0|


x = (0 or 0)
print(x)
# >> 0
x = (1 or 0)
print(x)
# >> 1
x = (1 or 1)
print(x)
# >> 1
x = (False or False)
print(x)
# >> False
x = (False or True)
print(x)
# >> True
x = (True or True)
print(x)
# >> True

NOT | Negation

NOT expressions are denoted with the symbols ¬ or ~.

If a = 1 then ~a = 0 or vice versa


x = (not False)
print(x)
# >> True
x = (not True)
print(x)
# >> False

Exclusive OR | Exclusive Disjunction | XOR

Exclusive OR expressions are denoted with the symbols or .

The or exclusivity refers to:

  • a or b but not a and b . They can never be both true at the same time.

|1 ⊻ 0 = 1| 0 ⊻ 1= 1|1 ⊻ 1= 0| 0 ⊻ 0 = 0|

If this leaves you a bit confused try this XOR sandbox http://xor.pw/ you will understand it in no time.


# To define XOR declarions in Python you can simply use the symbol ^
x = (0 ^ 0)
print(x)
# >> 0
x = (1 ^ 1)
print(x)
# >> 0
x = (1 ^ 0)
print(x)
# >> 1

NAND

NAND is a contraction of not and , meaning its result will always be the opposite of its AND expression. NAND is defined with the symbol .

a ↑ b = ~(a ∧ b)

|1 ↑ 0 = 1| 0 ↑ 1= 1|1 ↑ 1= 0| 0 ↑ 0= 1|


x = not(1 and 0)
print(x)
# >> True
x = not(1 and 1)
print(x)
# >> False
x = not(False and True)
print(x)
# >> True
x = not(True and True)
print(x)
# >> False

Implies | Conditional

As the name indicates there is a condition involved. Meaning if “If A then B” or “A implies B”. Implies are denoted with the symbol .

|0 → 0 = 1| 0 → 1 = 1|1 → 1 = 1|1 → 0 = 0|


def conditional(x, z):
  if x == True:
    return z
  return True

x = True
z = False

print(conditional(x, z))

If and Only If | Biconditional

A biconditional is a connective that represents the condition “if and only if”, by checking is both propositions have the same value. Biconditionals are denoted with the symbol .

A ↔ B = (A → B) ∧ (B → A)

|0 ↔ 0 = 1| 0 ↔ 1 = 0|1 ↔ 1 = 1|1 ↔ 0 = 0|


def main(a, b):

  def conditional(a, b):
    if a == True:
      return b
    return True

  A = conditional(a, b)
  B = conditional(b, a)
  if A == B:
    return True
  else:
    return False

a, b = True, False
print(main(a, b))

a, b = True, True
print(main(a, b))

a, b = False, True
print(main(a, b))

a, b = False, False
print(main(a, b))

Conclusion

Mathematical notation does not have to be complicated as you have seen. Now I leave it up to you as a challenge to try and create your own expressions and test them.

In case you are a Python developer try and create small algorithms using Propositional Logic Notation.

Bibliography

Propositional Logic | Brilliant Math & Science Wiki

Propositional calculus

Other Links on mathematical notation with Python

Mathematical Notation for Python Developers (Part 1)

Mathematical Notation for Python Developers (Part 2) | Sets continuation

Top comments (0)