Python Programming · Foundations

Conditionals

Want it in plain words first? Jump to Eli explains — the same idea, no jargon.
On this page 9 sections
  1. In 30 seconds
  2. Why this matters
  3. The college version
  4. Eli explains
  5. Worked example
  6. Key takeaway
  7. Quick check
  8. Study tools
  9. Sources & references

In 30 seconds

A is a decision: evaluate a , then choose an that matches its truth value. In Python, comparisons such as score >= 60 produce True or False, and other values can also be truth-tested. Conditionals let a program respond differently to its input or state instead of following one unchanging path.

Why this matters

Programs make decisions constantly: whether input is acceptable, whether a user may proceed, whether a value needs a fallback, or which message explains a result. Conditional reasoning turns a vague rule into a testable rule with defined outcomes. It also builds habits that matter in later programming work: state the condition precisely, test edge cases, and keep the condition separate from the action chosen after it. Those habits make code easier to test and explain.

The college version

A program decision has separate parts

A conditional gives a program more than one possible path. Its central idea is small but important: first evaluate a condition, then select an outcome that matches the result. The condition is a question about program state, such as whether a score meets a stated threshold. The outcome is what the program chooses after the question has an answer. Keeping those jobs distinct is useful. score >= 60 is a condition; the word 'eligible' is one possible outcome. Neither is a substitute for the other.

In Python, comparisons yield values, True or False. A condition can therefore be written as an expression whose truth value Python can determine. The Booleans lesson covers truth-value testing in depth: a condition need not literally contain True or False, because Python can test other objects in a Boolean context. Still, clear conditions usually make the rule visible. A name such as has_permission communicates a yes-or-no question more directly than a vague name such as value.

A conditional rule should say what happens on both sides of the decision. For a course rule, “a score of 60 or more is eligible; otherwise it needs review” has an explicit boundary and two outcomes. That is stronger than “high scores are eligible,” which leaves the threshold and the result for other scores unclear. The detailed statement syntax used to organize branches belongs to the next if-elif-and-else lesson. This lesson concentrates on the decision model that syntax expresses.

Conditions are rules, not labels

A condition must be evaluated from current values. Suppose score is 72. The expression score >= 60 evaluates to True; if score is 59, it evaluates to False. The itself answers the question. The values 72 and 59 are not Boolean objects, and the selected labels 'eligible' and 'review' are not the condition. Confusing those roles causes errors such as storing a result where a test was expected or using a display message as though it were evidence that a rule holds.

The design of the comparison matters. A rule that says “60 or more” needs >=, not >, because 60 is included. This is a : a value exactly on the edge of a rule. Testing at 59, 60, and 61 is a compact way to check that the condition matches its written policy. Likewise, a rule about an empty collection should test the property that matters—whether the collection is empty—not merely use a nearby-looking value. Python's truth-value testing can make an empty list useful as a condition, but it does not make an empty list equal to the Boolean object False.

Boolean operators can combine or negate conditions, but an operators catalog is outside this lesson. The conceptual point is enough here: a compound condition still needs a definite truth result, and the program should make clear which requirement is being tested. Write the policy in ordinary language first, identify its boundary, then express a condition that actually matches it. That workflow is more reliable than writing symbols first and guessing what rule they describe.

Selecting one of two values

Python also has a for choosing between two expressions: first_value if condition else second_value. Python evaluates the condition, then evaluates and returns the selected expression. For example, 'eligible' if score >= 60 else 'review' returns 'eligible' when the score is 60 or greater and 'review' otherwise. The result is a string because the two selected values are strings; the condition remains the Boolean result of score >= 60.

This form is most readable when both outcomes are short values and the choice is genuinely two-way. It is not a shortcut for every decision. When each outcome needs several operations, a named branch structure is generally clearer; that detailed form is intentionally reserved for the adjacent lesson. Similarly, repeated decisions over changing items belong with the lessons on loops, and a failure signaled by an operation belongs with exceptions. A conditional is about choosing based on a tested state, not about every way a program changes control flow.

Evaluation order matters. In a conditional expression, the condition is evaluated before Python selects an outcome, and only the selected outcome is evaluated. This lets a program avoid an expression that is irrelevant on the other side of the rule. The benefit is not an invitation to hide complicated work in a compact line. A good conditional has a readable condition, outcomes appropriate to the condition, and tests at the boundary. When those pieces are visible, another reader can check whether the program implements the intended rule rather than merely producing a plausible result on one example.

Eli, the EliExplains learning guide

Eli explains

The same idea, in plain words

Explain it like I’m 10

A conditional is like a rule at a school gate: first check the question, then choose what happens. If the rule is “a score of 60 or more may proceed,” the question is “is this score at least 60?” A score of 72 gets yes; a score of 59 gets no. The score, the yes-or-no answer, and the message chosen afterward are different things.

Python can use that yes-or-no answer to select one of two values. It checks the condition first and uses only the matching result. The tricky part is the edge: 60 itself. Because the rule says “60 or more,” 60 must go with the yes side. Trying numbers just below, at, and just above the edge helps catch mistakes.

Picture it like this

Think of a railroad switch controlled by a sensor. The sensor asks one question about the train's ticket, and the switch directs the train onto one of two tracks. The sensor's answer is the condition; the track chosen is the outcome.

Where the picture stops working

A real railroad switch moves a train, while a Python conditional can select a value or choose which code runs. Python conditions can also be more complex than one sensor question, and the analogy does not show the exact statement syntax used to organize branches.

Worked example

A program labels a score as eligible when it is at least 60 and as review otherwise. With score = 72, running result = 'eligible' if score >= 60 else 'review' produces 'eligible': the comparison 72 >= 60 is True, so Python selects the first string. With score = 59, the comparison is False, so the same expression produces 'review'. Test the boundary too: at score = 60, 60 >= 60 is True, which matches the stated rule. The condition is the comparison; result is the selected outcome.

Key takeaway

A conditional first evaluates a clear condition, then selects an outcome that matches True or False. Separate the rule from its result, and test the boundary values that define the rule.

Quick check

3 questions here, of 5 in this lesson’s practice set. Answers stay hidden until you check.

Question 1 of 3foundational

What does a Python comparison such as score >= 60 produce?

Choose an answer, then check it.
Question 2 of 3intermediate

A rule says “a score of 60 or more is eligible.” Which condition matches that rule?

Choose an answer, then check it.
Question 3 of 3intermediate

With score = 59, what does 'eligible' if score >= 60 else 'review' evaluate to?

Choose an answer, then check it.
Practice all 5

Keep learning

Ready to build on this? Continue to the next lesson.

Practice this lesson
Study tools & related lessonsYou’ll learn to · Common mistakes · Easily confused · Key vocabulary · Related

You’ll learn to

  • Define a conditional as a decision based on a truth-tested condition.
  • Explain how a comparison can supply a Boolean condition.
  • Distinguish a condition from the outcome selected by that condition.
  • Trace a two-outcome conditional expression.
  • Apply boundary-case reasoning to a simple program rule.

Common mistakes

  • Treating the selected message as the condition.

    Keep the truth-tested question separate from the value or action selected after it.

  • Using > for a rule that says “or more.”

    Use an inclusive comparison and test the exact boundary value.

  • Assuming a non-Boolean value is literally True or False.

    Python can truth-test a value without changing its type into a Boolean object.

  • Putting a multi-step decision into a terse conditional expression.

    Use a clearer branch structure when the outcomes need several operations.

Easily confused

condition vs. outcome

A condition is the truth-tested question; an outcome is the selected value or action.

`score >= 60` vs. `'eligible'`

The comparison yields a Boolean; the string is a possible result selected after that Boolean is known.

conditional expression vs. branch statement

A conditional expression selects one of two values inline; detailed statement-based branching is covered in the next lesson.

Key vocabulary

conditional
A program decision that selects an outcome after testing a condition.
condition
An expression whose truth value determines which outcome a conditional selects.
Boolean
A value of Python's bool type: True or False.
comparison
An expression that relates values and yields a Boolean result in Python.
outcome
The value or action selected after a conditional's condition is evaluated.
boundary case
An input at the exact edge of a rule, used to check whether the rule includes or excludes that edge.
conditional expression
Python's two-value selection expression, written x if condition else y.

Sources & references

  1. Built-in Types — Python 3 documentation — Python Software Foundation
  2. The Python Language Reference — 6. Expressions (Operator precedence) — Python Software Foundation
  3. The Python Tutorial — 4. More Control Flow Tools (if Statements) — Python Software Foundation

EliExplains lessons are original prose written from the open, credible references above. See Copyright & Licensing.

Researched 2026-08-19

Educational content only. It is not medical, legal or professional advice. Found an error? Tell us.