QuestenaPractice that shows what to review next
Topic lesson

Conditionals and comparison: evaluate only what Python reaches

About 6 min

Questions stay in the language in which they were published.

These questions ask which branch or operand Python evaluates and what a comparison means. Read expressions from left to right, apply truth testing, and stop precisely where the language stops.

Separate truth, control flow and returned values

An if, elif, else chain checks conditions in order, executes the suite for the first truthy condition, and skips the remaining alternatives. False, None, numeric zero and empty built-in strings or containers are falsy; nonempty strings and containers are truthy even when their contents look false-like. Try bool(list()): truth testing produces exactly False.

FormEvaluation method
not xReturn the Boolean opposite of x's truth value.
x and yIf x is falsy, return x without evaluating y; otherwise evaluate and return y.
x or yIf x is truthy, return x without evaluating y; otherwise evaluate and return y.
a if condition else bEvaluate the condition, then evaluate and return only the selected result expression.

Do not assume logical operators always return Boolean values: and and or return one of their operands. By contrast, bool and not always produce True or False. Try result = '' or 'ready': the empty first operand is falsy, so evaluation continues and returns the second string.

Equality asks whether values compare equal, while identity asks whether two references designate one object. Chained ordering such as a chained ordering check that value lies between low (exclusive) and high (inclusive) requires both neighbouring comparisons and evaluates the middle expression once. Numeric integers and floats can compare by numeric value.

String ordering is lexicographic by Unicode code points and remains case-sensitive; it does not automatically follow local dictionary conventions. Ordering unrelated built-in types such as an integer and a string raises TypeError rather than parsing or inventing a universal order. Compare the capital letter A with the small letter a using the less-than operator: the comparison follows their Unicode code-point order.

Reject the tempting wrong routes

TrapHow to reject it
Running every truthy branch in one if chainThe branches are alternatives. Stop after the first truthy condition; a later elif and the final else are skipped.
Judging truth from visible contents instead of emptinessZero and empty containers are falsy. A nonempty string or list is truthy even when it contains zero-like material.
Using equality and identity as interchangeable testsAsk whether the task concerns equal values or one shared object, then choose the corresponding relation.
Evaluating both operands of and or or and forcing a Boolean resultUse the first operand's truth value to decide whether evaluation stops, and return the selected operand itself.
Comparing the first and final terms in the second half of a chainSplit a chain into neighbouring comparisons that share the middle expression; both must hold.
Evaluating both possible results of a conditional expressionTest the condition first. The unselected expression is skipped, just like an unchosen branch.

Try it

Question 58

What is the result of `3 == 3.0` in Python?

  1. `True`, because the numeric values are equal
  2. `False` because the operand types differ
  3. A `TypeError` because Python does not auto-convert them
  4. `3.0` because the integer is converted and returned
Question 59

For ordinary Python string ordering, which statements are true? Select all that apply.

  1. It automatically follows the current locale's dictionary order.
  2. It compares lexicographically by Unicode code points.
  3. It ignores letter case without being asked.
  4. Uppercase and lowercase can affect the ordering result.
Question 60

What happens when Python evaluates `1 < "2"`?

  1. `True` after converting the string to an integer
  2. `False` because all strings sort before integers
  3. A `TypeError` because Python does not auto-convert them
  4. A stable result based on the objects' memory locations
Start drillPractice this topic
Conditionals and comparison: evaluate only what Python reaches · Questena