Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Conditional Statements and Decision Making

Teach Python how to choose the next action when business rules, thresholds, or changing inputs demand different outcomes.

This notebook builds directly on variables, data types, and operators. Once you can store values and compare them, the next step is deciding what should happen when a condition is true, false, or only partly true.

Control flow lets a program make decisions. Instead of running every line the same way every time, Python can evaluate conditions and choose what should happen next.

This matters whenever a rule changes the outcome: approve or reject a discount, flag risky transactions, label customers, or decide whether a model score is acceptable.

Why This Matters

Business workflows
Machine learning workflows
Software thinking

Discount approval, stock alerts, order routing, and fraud checks all depend on conditions that change the next action.

Concept Explanation

Conditionals are useful whenever different situations require different actions. A conditional statement asks a question that evaluates to True or False, then runs the branch that matches the answer.

For example, you might:

  • approve or reject a discount

  • classify a customer as active or inactive

  • decide whether inventory is low

  • determine whether a model score should trigger review

Reading the logic
  • Python checks whether sales >= 10000

  • if the answer is True, the first branch runs

  • otherwise, the else branch runs


Worked Example: Order Review Workflow

A business might route orders differently depending on their value. Small orders can be processed normally, medium ones may need an extra check, and very large ones may need manual approval.

Why elif matters

elif lets you test another condition only when the earlier one was not true. That helps you express ranked decision rules without writing separate disconnected if statements.

Quick Quiz

When should you use an if statement?

When a program needs to choose between actions based on a conditionCorrect. Conditionals make decisions explicit.
When you want to repeat a task many timesThat is usually the job of a loop.
When you want to import a libraryImports are handled with import, not if.
risk_score = 82

if risk_score >= 85:
    print("Escalate for immediate review")
elif risk_score >= 60:
    print("Queue for analyst review")
else:
    print("Approve automatically")
Queue for analyst review

Guided Exercise

Use conditionals to design a simple loyalty decision rule for a store.

purchase_total = 920
is_member = True

# TODO: if the customer is a member and spends at least 900, print "VIP reward"
# TODO: otherwise if the customer is a member, print "Member discount"
# TODO: otherwise print "Standard pricing"
Hint

Start by checking the most specific case first. If you reverse the order, a broader condition may capture the case too early.

One possible solution
purchase_total = 920
is_member = True

if is_member and purchase_total >= 900:
    print("VIP reward")
elif is_member:
    print("Member discount")
else:
    print("Standard pricing")

Quick Summary

Key Takeaways
  • conditionals help programs choose between actions instead of running one fixed path

  • if, elif, and else let you express ranked decision rules clearly

  • comparisons produce boolean results that drive branch selection

  • indentation tells Python which lines belong to each branch

The next notebook extends this idea by showing how Python can repeat a decision or action across many values with loops.

Interactive Code

Expected output
High performance
Expected output
Low stock

Guided Practice

Which keyword starts a second condition after if?

loopThat is not a Python keyword for conditions.
elifCorrect. elif adds another conditional branch.
thenPython does not use then in if statements.
switchThat is not the standard keyword used here.

What prints when inventory = 5 in the example above?

Out of stockThat would happen only when inventory equals 0.
Low stockCorrect. 5 is less than 10 and greater than 0.
Stock is healthyThat would happen with a larger inventory.
NothingOne branch will execute.

Extension Prompt

Write a conditional that checks a model confidence score and prints one of three outcomes: "accept", "review", or "reject". Keep the thresholds simple now. The next notebook on loops will show how to apply the same rule across many records instead of just one value.