Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 'not(True) * True' valid syntax, unlike 'True * not(True)'? [duplicate]

Is this a consequence of the hierarchical order of operators in Python?

not(True) * True
# False
True * not(True)
# SyntaxError: invalid syntax
like image 210
jose rosales Avatar asked Nov 19 '25 10:11

jose rosales


1 Answers

Is this a consequence of the hierarchical order of operators in Python?

Yes (although the usual term is operator precedence). Summing up and simplifying:

  1. not isn't a function; it's an operator. Therefore, we don't need to write parentheses for not (True), and in fact they don't do anything here. All that happens is that the parentheses are treated as ordinary grouping parentheses - (True) is evaluated before anything else, becoming True. So, let's consider the examples without the parentheses.

  2. not True * True means not (True * True). It does not mean (not True) * True, due to operator precedence. This is by design:

    >>> not 1 * 0
    True
    >>> not (1 * 0)
    True
    >>> (not 1) * 0
    0
    

    It would, the developers figured, be unexpected to write something like not 1 * 0 and get an integer result, and unexpected to write not in front of a mathematical operation and have the not only apply to the first thing in that expression.

  3. Because of that same operator precedence, True * not True is a syntax error. Python parses the not by itself as the right-hand side of the *, because it hasn't yet worked out to put not True together. True * not is obviously nonsense. Or, another way of looking at it: "not followed by an expression" isn't in the list of "things that can be an operand for *".

    This is perhaps surprising because the other commonly used unary operator, - (i.e., unary negation), doesn't have this issue. But that's because the precedence is the other way around: unary negation is processed before multiplication, not after.

    The same is true for and and or combinations:

    >>> 3 * 5 and 1 # 3 * 5 is evaluated first
    1
    >>> 3 * (5 and 1)
    3
    >>> 3 or 1 * 5 # 1 * 5 is evaluated first, even though it comes later
    3
    >>> (3 or 1) * 5
    15
    
like image 167
Karl Knechtel Avatar answered Nov 22 '25 00:11

Karl Knechtel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!