Module 5 · Conditional Statements — topic 1 of 10
Decision-making
Programs become useful when they can choose between actions. Learn how a condition controls which code runs, how to describe a decision before coding it, and how Python represents choices.
Learning objectives
- Explain what a decision in a program is
- Describe a decision as a condition plus actions
- Plan a decision in plain words before writing code
Definition
Programs become useful when they can choose between actions. Learn how a condition controls which code runs, how to describe a decision before coding it, and how Python represents choices.
Introduction
So far your programs have run every line from top to bottom. Real programs need to react to situations: show an error if a password is too short, give a discount only to members, print “pass” or “fail” depending on the marks. This is decision-making, also called selection or branching.
A decision has two parts: a condition, which is a question with a True or False answer, and the actions to take for each answer. Python’s if statement puts these two parts together.
Why it matters
Almost every requirement you will ever implement contains the word “if”: if the stock is low, reorder; if the user is not logged in, redirect; if the input is invalid, ask again. Being able to turn such sentences into correct code is the single most important step from simple scripts to useful programs.
Detailed explanation
Control flow is the order in which statements run. Without decisions it is a straight line. A decision splits the line into branches, and only one branch runs each time the program reaches that point.
The condition can be any expression. Python evaluates it and treats the result as true or false. Comparisons such as marks >= 40 are the most common conditions, but variables, function calls and combinations with and/or are also used.
Before coding, write the decision in plain words or as a small table listing each situation and the action it needs. This exposes missing cases early. For example, a ticket price rule needs separate actions for children, adults and seniors, and you must decide where exactly each age boundary falls.
Python provides three shapes of decision: if (do something only when true), if-else (choose between two actions) and if-elif-else (choose one of many). The following lessons cover each in turn.
Syntax
- The keyword if starts the decision.
- The condition is followed by a colon.
- The indented block is the branch that runs when the condition is true.
- Execution continues with the next unindented statement either way.
Code examples
One simple decision
Heat warning issued Forecast complete
The condition is just a value
False Result recorded
More examples
Turning a decision table into code
8 -> 100 12 -> 200 35 -> 200 59 -> 200 60 -> 120 72 -> 120
Step-by-step explanation
- ticket_price checks the cheapest special case first: children under 12.
- return ends the function immediately, so later checks are skipped for children.
- Anyone reaching the second if is at least 12, so age < 60 alone identifies adults.
- Everyone else must be 60 or over and gets the senior price.
- The test ages include both sides of each boundary (12, 59 and 60).
Expected output
Running “Turning a decision table into code” prints exactly:
Practical applications
- Online stores decide shipping charges from the order total and destination.
- Banking apps block a withdrawal if the balance is too low.
- Games decide whether a player has won, lost or should keep playing after every move.
- Thermostats switch heating on or off by comparing temperature with a target.
Best practices
- Describe each decision in words or a table before coding it.
- Test values on both sides of every boundary.
- Keep conditions short; store complicated ones in a named Boolean.
Common mistakes
Coding before deciding the boundaries
Avoid
Prefer
Writing the rule in a table first makes you decide which group each boundary value belongs to.
Forgetting the colon
Avoid
Prefer
Every if line ends with a colon; without it Python reports a SyntaxError.
Key takeaways
- A decision is a condition plus the actions for each outcome.
- Only one branch of a decision runs each time.
- Plan boundaries carefully and test them.
Try it yourself
- 1Write a decision table for a mobile data plan: under 1 GB used is “low”, 1–5 GB is “medium”, above 5 GB is “high”. Then code it.
- 2Write a program that prints “Free delivery” only when an order total is at least 499.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
10 questions cover the “Conditional Statements” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?