Lecture 3.1 · Interactive lab

Decisions Lab

See which branch runs, build truth tables, and write your own spam rules

Introduction to Data Science and Machine Learning
Md Mahbub E Noor · University of Barishal
All labs

Grade machine: which branch runs, and why order matters

Move the score. Python checks the conditions from the top and stops at the first True. Then switch to the wrong order and watch whole grades disappear.

The path Python takes

Every score from 0 to 100 with this code

050100

Boundary tests: the scores where bugs hide

What to notice

  • Only one branch of an if / elif / else chain ever runs.
  • Conditions after the first True are not checked at all.
  • With the loosest condition first, A and B can never be printed: the code runs without error but is wrong.
  • Test the edges: exactly 80, 79, 65, 64, 50, 49.

Try this

and · or · not: build conditions from smaller ones

Flip the two switches. Every expression below is recalculated. Click an expression to see its full truth table.

p =
q =

Your own expression (use p and q)

Truth table:

Real use: who can enter?

What to notice

  • and is True only when both sides are True; or when at least one is.
  • not (p and q) always equals not p or not q. Compare their columns.
  • Each side of and must be a complete condition: age >= 18 and <= 60 is a SyntaxError.

Try this

Spam rule tester: write a rule, test it on an inbox

Pick suspicious words. The rule flags a message if it contains them. See which spam it catches, which it misses, and which normal messages it wrongly flags.

Suspicious words (click to switch on or off)

Spam caught
Spam missed
False alarms

Test your own message

The inbox (label = what a human decided)

What to notice

  • More words with or catch more spam and raise more false alarms.
  • Switching to and makes the rule strict: few false alarms, but most spam slips through.
  • Some spam (FR33 g1ft) beats every word rule. Lecture 6 lets the computer learn the rule from examples instead.

Try this

match-case: one value, many fixed options

Type a phone number. Python looks at the first three characters and tries each case from the top, just like an elif chain.

Output

The same decision with if / elif

What to notice

  • number[:3] is the first three characters, as text: "017", not the number 17.
  • | inside a case means or; case _: catches everything else, like else.
  • Without the else:, a short number prints INV and then an operator name.

Try this

Works offline: no internet needed. The Python shown here runs in your browser with the same rules as Python 3, so results and error messages match what you will see in Jupyter.