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.
| Form | Evaluation method |
|---|---|
| not x | Return the Boolean opposite of x's truth value. |
| x and y | If x is falsy, return x without evaluating y; otherwise evaluate and return y. |
| x or y | If x is truthy, return x without evaluating y; otherwise evaluate and return y. |
| a if condition else b | Evaluate 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
| Trap | How to reject it |
|---|---|
| Running every truthy branch in one if chain | The 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 emptiness | Zero 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 tests | Ask 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 result | Use 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 chain | Split a chain into neighbouring comparisons that share the middle expression; both must hold. |
| Evaluating both possible results of a conditional expression | Test the condition first. The unselected expression is skipped, just like an unchosen branch. |
Try it
What is the result of `3 == 3.0` in Python?
- `True`, because the numeric values are equal
- `False` because the operand types differ
- A `TypeError` because Python does not auto-convert them
- `3.0` because the integer is converted and returned
For ordinary Python string ordering, which statements are true? Select all that apply.
- It automatically follows the current locale's dictionary order.
- It compares lexicographically by Unicode code points.
- It ignores letter case without being asked.
- Uppercase and lowercase can affect the ordering result.
What happens when Python evaluates `1 < "2"`?
- `True` after converting the string to an integer
- `False` because all strings sort before integers
- A `TypeError` because Python does not auto-convert them
- A stable result based on the objects' memory locations