[Explained] Why is ‘not(True) * True’ valid syntax, unlike ‘True * not(True)’?

The question “Why is ‘not(True) * True’ valid syntax, unlike ‘True * not(True)'” is an question related to the the hierarchical order of operators in Python and is very common question when you are working on python.

The article below will in detail discuss the question “Why is ‘not(True) * True’ valid syntax, unlike ‘True * not(True)’?” and will help you understand operators on a much deeper level.

Why is ‘not(True) * True’ valid syntax, unlike ‘True * not(True)’?

The question “Why is ‘not(True) * True’ valid syntax, unlike ‘True * not(True)’? ” arises because python’s operators are arranged in a hierarchical fashion as a result.

The usual term to define this is operator precedence, you can read more about operator precedence here.

Follow the article below to understand the question through some helpful examples.

Not is an operator; it is not a function. As a result, we don’t need to use parenthesis for not (True), and in this case, they serve no use.

The parentheses are just interpreted as regular grouping parentheses, with (True) being evaluated first and becoming True. So, without the parentheses, let’s think about the examples.

Not (True * True) is the opposite of True * True. Because of operator precedence, it does not mean (not True) * True. Look at the below mentioned example to understand this:

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

The creators believed that it would be unexpected to type not before a mathematical operation and have the not only apply to the first item in that expression, as well as unexpected to type not 1 * 0 and receive an integer result.

The syntax error in True * not True is caused by the same operator precedence. Python doesn’t yet know how to combine not True, so it parses the not by itself as the right-hand side of the *. True * not is clearly absurd.

Or, to put it another way, “not followed by an expression” is not included in the list of “items that can be an operand for *.”

This may come as a surprise given that the other often used unary operator, – (sometimes known as the unary negation), doesn’t have this problem.

But the reason for this is that unary negation is handled before multiplication rather than the other way around.

The same holds true for the combinations of and and or as you can see in the below mentioned examples:

>>> 7 * 9 and 1 # being examined first is 7 * 9
>>> 7 * (9 and 1)
7
>>> 7 or 1 * 9 # being examined first is 1 * 9, even though it comes after
7
>>> (7 or 1) * 9
63

This should help you understand the question “Why is ‘not(True) * True’ valid syntax, unlike ‘True * not(True)’?”.

Conclusion

The syntax error in True * not True is caused by the same operator precedence. Python doesn’t yet know how to combine not True, so it parses the not by itself as the right-hand side of the *.

True * not is clearly absurd. Or, to put it another way, “not followed by an expression” is not included in the list of “items that can be an operand for *.”