From two truth values to a digital circuit: formulate logical statements, check them systematically, and implement them as Python functions.
Module 1 — Truth Values in Python
Boolean variables have exactly two states: true or false.
From switching states to bool
In propositional logic, True and False stand for true and false. Digital circuits often write the same states as 1 and 0. Python maps both representations to each other: int(True) returns 1, int(False) returns 0.
tuer_zu = True stoerung = False print(int(tuer_zu), int(stoerung)) # 1 0
Example 1.1 — Truth Values and Numbers
Loading Python…Tests
Module 2 — NOT, AND, OR, and XOR
Logical operators combine truth values according to fixed rules.
The Basic Operations
not Ainverts a truth value.A and Bis true only when both inputs are true.A or Bis true when at least one input is true.A != Bimplements XOR: exactly one input must be true.
A truth table lists all input combinations. For two variables, there are four rows, which can be generated reliably with two nested loops.
Example 2.1 — Truth Table
Loading Python…Tests
Module 3 — Boolean Laws
Laws help check and simplify logical expressions.
Axioms and Important Consequences
The commutative, distributive, identity, and complement laws form the basis of Boolean algebra. Other important laws include associativity, double negation, absorption, and the two De Morgan laws.
not (A and B) == ((not A) or (not B)) not (A or B) == ((not A) and (not B))
An equation is a logical law only if both sides produce the same truth value for every possible assignment. A truth table verifies exactly that.
Example 3.1 — Verify De Morgan Completely
Loading Python…Tests
Module 4 — From Expression to Circuit
Logical functions describe digital building blocks.
Half Adder and Seven-Segment Display
A half adder adds two single bits. The carry is true when both bits are set — an AND. The sum bit is true when exactly one of the two bits is set — an XOR, as the table shows:
| x | y | Sum (XOR) | Carry (AND) |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
Python has no dedicated XOR keyword for truth values. From the table, however, the expression can be assembled with AND, OR and NOT: read off the rows in which the sum is 1 — that is exactly the task in part 4.
A seven-segment display is also a logical function: three input bits encode the digits 0 to 7, and each segment receives its own on or off value from them.
Example 4.1 — Half Adder
Loading Python…Tests
Module 5 — DNF and CNF
Normal forms systematically translate truth tables into expressions.
Disjunctive and Conjunctive Normal Form
For disjunctive normal form, all table rows with output 1 are formulated as AND terms and then joined with OR. For conjunctive normal form, rows with output 0 are formulated as OR terms and joined with AND.
This always produces an executable logical expression from a complete truth table. The Boolean laws can then simplify the expression.
The example assembles the disjunctive normal form for XOR from the two table rows with output 1 and checks it against !=.
Example 5.1 — DNF from the truth table
Loading Python…Tests
PracticeNow Program It Yourself
The five exercises lead from a generated truth table via De Morgan and the seven-segment display to the half adder and a machine release with a maintenance case.
Sources and Further Reading
Module sources and further Python references.
Teaching Material and Documentation
- Lecture notebook IngInfo_WS26_27_VÜ.ipynb, sections on propositional logic, Boolean algebra, the half adder, DNF, and CNF.
- Python Language Reference — Boolean operations.
- Real Python — Python Booleans.
- Theis, Thomas: Getting started with Python. 7th edition, Rheinwerk Verlag, 2023.