#================================================================================= # Updated Table 5-2 for Learning Python, 5th Edition (revised November 2019). # # This version does not change any of the original version's content. Its text # and text formatting (not shown here) are unchanged, except for the trivial # deletion of a few ";" in column 2 (better row distinction makes these moot). # # Its only significant changes are to: # (1) Make row membership more obvious, because it's crucial to precedence # (2) Coalesce a few formerly adjacent but separate rows for improved accuracy # # This now closely matches Python's docs, though recent Python extensions such # as "@" and ":=" are naturally absent; this still doesn't codify things like # "*" packing and function argument lists; and Python's docs are still informal # on this front too. See Python's source-code Grammar file for full fidelity. # # The book itself achieved change #1 above by adding borders around table cells; # see learning-python.com/LP5E-Table-5-2-{Before, After}.png for screenshots. #================================================================================= Table 5-2. Python expression operators and precedence Operators Description ------------------------------ ---------------------------------------------------- yield x Generator function send protocol ------------------------------ ---------------------------------------------------- lambda args: expression Anonymous function generation ------------------------------ ---------------------------------------------------- x if y else z Ternary selection (x is evaluated only if y is true) ------------------------------ ---------------------------------------------------- x or y Logical OR (y is evaluated only if x is false) ------------------------------ ---------------------------------------------------- x and y Logical AND (y is evaluated only if x is true) ------------------------------ ---------------------------------------------------- not x Logical negation ------------------------------ ---------------------------------------------------- x in y, x not in y Membership (iterables, sets) x is y, x is not y Object identity tests x < y, x <= y, x > y, x >= y Magnitude comparison, set subset and superset x == y, x != y Value equality operators ------------------------------ ---------------------------------------------------- x | y Bitwise OR, set union ------------------------------ ---------------------------------------------------- x ^ y Bitwise XOR, set symmetric difference ------------------------------ ---------------------------------------------------- x & y Bitwise AND, set intersection ------------------------------ ---------------------------------------------------- x << y, x >> y Shift x left or right by y bits ------------------------------ ---------------------------------------------------- x + y Addition, concatenation x – y Subtraction, set difference ------------------------------ ---------------------------------------------------- x * y Multiplication, repetition x % y Remainder, format x / y, x // y Division: true and floor ------------------------------ ---------------------------------------------------- −x, +x Negation, identity ˜x Bitwise NOT (inversion) ------------------------------ ---------------------------------------------------- x ** y Power (exponentiation) ------------------------------ ---------------------------------------------------- x[i] Indexing (sequence, mapping, others) x[i:j:k] Slicing x(...) Call (function, method, class, other callable) x.attr Attribute reference ------------------------------ ---------------------------------------------------- (...) Tuple, expression, generator expression [...] List, list comprehension {...} Dictionary, set, set and dictionary comprehensions ------------------------------ ----------------------------------------------------