Mascot image.
#EECS#Python#Logic

From statements to branches

In mathematics, a statement is something that is either true or false.

Examples:

  • 3<53 < 5 is true.
  • 1010 is even is true.
  • n2<nn^2 < n is not always true; it depends on the value of nn.

In programming, these become Boolean expressions.

3 < 5
x % 2 == 0
score >= 50

Each of these evaluates to either True or False.

That is exactly why conditionals matter: a program can choose a branch based on the truth value of a predicate.

The shape of an if

age = int(input("Age: "))

if age >= 18:
    print("adult")
else:
    print("minor")

This is not philosophically complicated. It is just a decision tree written in a strict syntax.

The mathematical version is the piecewise rule

status(a)={adult,a18,minor,a<18.\text{status}(a) = \begin{cases} \text{adult}, & a \ge 18, \\ \text{minor}, & a < 18. \end{cases}

Python lets us execute that rule.

Combining predicates

You will constantly use and, or, and not.

For example:

if score >= 50 and attendance >= 0.8:
    print("pass")

This means both conditions must be satisfied.

Mathematically, if

P:=(score50),Q:=(attendance0.8),P := (\text{score} \ge 50), \qquad Q := (\text{attendance} \ge 0.8),

then the branch runs exactly when PQP \land Q is true.

Example: sign of a number

def sign(x: float) -> str:
    if x > 0:
        return "positive"
    elif x < 0:
        return "negative"
    else:
        return "zero"

This looks trivial, but it is already a model of disciplined casework.

Whenever a mathematical problem splits into disjoint cases, code often mirrors that structure directly.

Guardrails for beginners

A few mistakes show up constantly:

  1. Writing = when you meant ==.
  2. Forgetting indentation.
  3. Writing overlapping conditions in the wrong order.

For example:

score = 95

if score >= 50:
    print("pass")
elif score >= 90:
    print("distinction")

This never prints "distinction" because the first branch already captures every score above 90.

The fix is to order conditions from most restrictive to least restrictive.

Short exercise set

Write functions for the following:

  1. is_even(n) returning True iff nn is even.
  2. max_of_two(a, b) returning the larger input.
  3. grade(mark) returning "A", "B", "C", or "F" using sensible cutoffs.

Then explain the logic of each one in plain English before you run it.

That last step matters. If you cannot explain the branch structure, you usually do not understand it yet.

Good code branches only work when the reasoning behind them is explicit.