What does 8 / 4 return in Python 3?
True division always returns a float. Use 8 // 4 for the int 2.
Operators and Expressions · Exercise 1 of 4
Fifteen multiple-choice questions on Python operators: division, modulo and power, precedence, chained comparisons, logical and bitwise operators, identity, membership and the walrus operator. Pick an option to check your answer instantly.
What does 8 / 4 return in Python 3?
True division always returns a float. Use 8 // 4 for the int 2.
What is the value of -9 // 4?
-9 / 4 = -2.25, and floor rounds toward minus infinity, giving -3. C and Java would give -2.
What is -10 % 3 in Python?
-10 // 3 is -4, and -4 × 3 + 2 = -10, so the remainder is 2 – it takes the sign of the divisor (3).
How is 2 ** 3 ** 2 evaluated?
** is the only right-associative binary operator, so the rightmost power is computed first.
What is the value of -3 ** 2?
** binds tighter than unary minus: -(3 ** 2) = -9. Write (-3) ** 2 for 9.
What does 0 < x <= 100 mean in Python?
Comparisons chain naturally, as in maths, and x is evaluated only once.
What is the value of "" or "Guest"?
or returns the first truthy operand (or the last one). The empty string is falsy, so the result is "Guest".
In x != 0 and 10 / x > 1, what happens when x is 0?
x != 0 is False, so and stops there without evaluating its right side. This guard pattern is common.
What does 5 ^ 2 compute in Python?
^ is bitwise exclusive OR. Powers use **: 5 ** 2 is 25.
What is ~7?
Bitwise NOT of x equals -(x + 1) in two's complement, so ~7 is -8.
Timed online tests with instant scores and explanations.