Python Operators
Python Operators Interview Questions
| Operator | Name | Example |
|---|---|---|
| + | Addition | 5 + 3 = 8 |
| - | Subtraction | 5 - 3 = 2 |
| * | Multiplication | 5 * 3 = 15 |
| / | Division | 5 / 2 = 2.5 |
| % | Modulus | 5 % 2 = 1 |
| ** | Exponentiation | 5 ** 2 = 25 |
| // | Floor Division | 5 // 2 = 2 |
| Operator | Name | Example |
|---|---|---|
| == | Equal to | 5 == 5 → True |
| != | Not equal to | 5 != 3 → True |
| > | Greater than | 5 > 3 → True |
| < | Less than | 5 < 3 → False |
| >= | Greater than or equal | 5 >= 5 → True |
| <= | Less than or equal | 5 <= 3 → False |
| Operator | Name | Description |
|---|---|---|
| and | Logical AND | True if both operands are true |
| or | Logical OR | True if at least one operand is true |
| not | Logical NOT | True if operand is false |
| Operator | Example | Equivalent to |
|---|---|---|
| = | x = 5 | x = 5 |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| %= | x %= 3 | x = x % 3 |
| //= | x //= 3 | x = x // 3 |
| **= | x **= 3 | x = x ** 3 |
| &= | x &= 3 | x = x & 3 |
| |= | x |= 3 | x = x | 3 |
| ^= | x ^= 3 | x = x ^ 3 |
| >>= | x >>= 3 | x = x >> 3 |
| <<= | x <<= 3 | x = x << 3 |
| Operator | Name | Example (a=5, b=3) |
|---|---|---|
| & | AND | 5 & 3 = 1 (0101 & 0011 = 0001) |
| | | OR | 5 | 3 = 7 (0101 | 0011 = 0111) |
| ^ | XOR | 5 ^ 3 = 6 (0101 ^ 0011 = 0110) |
| ~ | NOT | ~5 = -6 |
| << | Left shift | 5 << 1 = 10 (0101 → 1010) |
| >> | Right shift | 5 >> 1 = 2 (0101 → 0010) |
| Operator | Description | Example |
|---|---|---|
| is | True if both variables point to same object | x is y |
| is not | True if variables point to different objects | x is not y |
| Operator | Description | Example |
|---|---|---|
| in | True if value exists in sequence | x in [1,2,3] |
| not in | True if value doesn't exist in sequence | x not in [1,2,3] |
2. In Python, bool is a subclass of int: True == 1 and False == 0. Adding them uses integer addition, not logical OR.
or returns the first operand that is truthy, otherwise the last. An empty list is falsy, so evaluation skips to [1]. This is short-circuiting with values preserved, not converted to bool.
512. Exponentiation is right-associative—the rightmost ** groups first.
2 ** 3 ** 2
# same as 2 ** (3 ** 2) → 2 ** 9 → 512
(2 ** 3) ** 2 # if you need left grouping → 8 ** 2 → 64+= on lists calls extend-like behavior in place (__iadd__), not rebinding a to a new object. Other names referencing the same list see the mutation.
Yes. PEP 8 recommends is for None because only one None exists. == can be overridden by a weird __eq__; is tests identity only.
16. Left shift multiplies by 2 ** n: 1 * 2**4. Useful for fast powers of two; beware overflow in fixed-width languages—Python ints are arbitrary precision.
CPython caches small integers (typically -5 to 256). Equal value does not imply same object for bigger ints—always use == for numeric equality.
-2. Bitwise NOT uses the underlying int: ~x == -(x+1). So ~1 == -2. Do not confuse with logical not True which is False.
and returns the first falsy value or the last operand. Here 0 is falsy, so expensive() is never called—useful for guarding expensive checks.
True. Chained comparisons expand with and; the middle operand is evaluated once and reused.
3 < 5 > 2
# equivalent to:
(3 < 5) and (5 > 2)
# idiomatic range check:
lo < x < hiUse isinstance(x, list). Subclasses (e.g. custom list-like types) satisfy isinstance but fail exact type(...) == checks—better polymorphism.
1. Modulo result shares sign with divisor (always non-negative here). Unlike some languages where remainder follows dividend sign, Python’s % is consistent with // floor division.
False. NaN is not equal to itself per IEEE rules. Use math.isnan(x) to test. Similarly tricky in sorting and dict keys.
Bitwise XOR assignment—classic swap trick with integers (pairwise XOR). In Python prefer tuple unpacking x, y = y, x for clarity unless teaching bitwise tricks.
True. Vacuous truth: no failing elements. Similarly any([]) is False. Interviewers use this to probe understanding of quantifiers.
True then False. Equal contents vs different objects. Same for two fresh empty dicts.
As not (x == y) because == binds tighter than not. Use parentheses when mixing and/or/not with comparisons to avoid surprises.
Floor division rounds toward minus infinity: -3 // 2 == -2, not trunc toward zero. Pair it with % so (a // b) * b + (a % b) == a.
& is bitwise AND on ints/bools as bits; and is logical short-circuit returning operands. On booleans they coincide numerically but types/paths differ.
False. Fresh user objects compare unequal by default unless classes define __eq__. Identity differs too.
{**a, **b} or a | b (3.9+) shallow-merges keys; later keys overwrite. Order reflects insertion rules for dicts.
Since Python 3.5, @ is matrix multiplication (__matmul__)—mainly for NumPy. Built-in scalars do not implement it by default.
True. Non-empty string is truthy—the characters spell “False” but the string itself exists. Use distutils.util.strtobool or explicit parsing for config strings.
Implementation may intern some strings, but never rely on is for string equality—use ==. Identity tricks break across interpreters/builds.
(-3, 1). Quotient floors toward negative infinity; remainder matches %. Together they reconstruct -11.
Yes when called—or inside still short-circuits left-to-right at call time. Lambdas don’t change evaluation rules inside the expression.
It chains as (x == y) and (y == z), which implies transitivity for equality but still evaluates y twice—usually fine; contrast is chains which are rarer.
False, numeric value 0. Printed as False. Mixing bitwise and logical operators mentally causes confusion—keep contexts separate.
Not operator precedence—sort stability and key ties—items with equal keys keep relative order (stable sort). Combine with secondary keys from tuples if needed.
Lets you assign and test in one expression: if (n := len(s)) > 5:. Avoid duplicating expensive calls—still respect readability.
False. Empty list does not equal boolean False despite both being falsy in boolean context—== compares types/values, not truthiness.