How does Python identify code blocks?
Python uses indentation to identify which statements belong to an `if`, `while`, or `for` block. Statements in the same block must be indented consistently.
Study 3 Control Flow with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
How does Python identify code blocks?
Python uses indentation to identify which statements belong to an `if`, `while`, or `for` block. Statements in the same block must be indented consistently.
What do Python's `and`, `or`, and `not` operators do?
`and` is true when both operands are true; `or` is true when at least one operand is true; `not` reverses a truth value.
What is short-circuit evaluation?
Short-circuit evaluation stops `and` when its left operand is false and stops `or` when its left operand is true, so unnecessary or unsafe expressions may not run.
Which common Python values are falsy?
Falsy values include `False`, `None`, numeric zero, and empty strings or collections. Most other objects are truthy.
When should Python use `==` rather than `is`?
Use `==` to compare values. Use `is` mainly for object identity checks involving singletons such as `None`.
What does `0 <= score <= 100` mean?
A chained comparison such as `0 <= score <= 100` expresses both comparisons and evaluates the middle expression only once.
How does Python choose among `if` and `elif` branches?
Python tests `if` and `elif` conditions from top to bottom and executes only the first matching branch. Remaining branches are skipped.
When does a `while` loop execute its body?
A `while` loop checks its condition before every iteration. If the condition is initially false, its body runs zero times.
When is a `while` loop appropriate?
Use a `while` loop when repetition depends on a condition that changes during execution or when the number of repetitions is not known in advance.
How does a Python `for` loop traverse data?
A `for` loop assigns each item from an iterable to a loop variable and runs its body. It ends when the iterable is exhausted.
How can a `for` loop obtain both index and item?
`enumerate()` supplies both an index and an item, as in `for index, name in enumerate(names):`.
Is the stop value included in `range()`?
The `stop` value in `range()` is exclusive. For example, `range(5)` produces the integers 0 through 4.