What is an expression in Python?
An expression is code evaluated to produce a value; it may contain literals, variables, operators, and function calls.
Study 03. Expressions and Boolean Logic with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is an expression in Python?
An expression is code evaluated to produce a value; it may contain literals, variables, operators, and function calls.
In `a + b`, what are the operator and operands?
The operator performs the operation, while the operands are the values on which it operates. In `a + b`, `+` is the operator and `a` and `b` are operands.
What is the result of `7 // 2`?
`7 // 2` evaluates to `3` because floor division returns the floor of the quotient.
What does `7 % 2` evaluate to?
`7 % 2` evaluates to `1`; the `%` operator returns the remainder after division.
How do `=` and `==` differ in Python?
`=` assigns a value, while `==` tests whether two values are equal. For example, `x = 10` assigns; `x == 10` compares.
What does `10 <= score <= 20` mean?
The chained comparison is equivalent to `10 <= score and score <= 20`. It checks that `score` lies from 10 through 20, inclusive.
What do Python's `not`, `and`, and `or` operators do?
`not` reverses a truth value, `and` is true when both conditions are true, and `or` is true when at least one condition is true.
Which common Python values are false in a Boolean context?
`False`, `None`, numeric zero, and empty strings and containers are false in Boolean contexts; most other values are true.
When do `and` and `or` short-circuit?
`A and B` stops when `A` is false, while `A or B` stops when `A` is true, because the final result is already determined.
What can Python's `and` and `or` return?
Python's `and` and `or` return one of their operands, not necessarily `True` or `False`. Thus, `entered_name or "Guest"` produces `"Guest"` when `entered_name` is empty.
How does `+` combine strings in Python?
The `+` operator concatenates strings, and both operands must be strings. Convert other types with `str()` or use a formatted string.
What is the result of `"-" * 5`?
`"-" * 5` evaluates to `"-----"`; multiplying a string repeats it the specified number of times.