Python Python Order of Operations: Unary Negation Explained

  • Thread starter Thread starter Mr Davis 97
  • Start date Start date
  • Tags Tags
    Operations
Click For Summary
The discussion focuses on the order of operations in Python, particularly the unary negation operator and its right-to-left precedence. It clarifies that the right-to-left association means expressions like `~~x` are evaluated as `~(~x)`. An example illustrates that `-~0` evaluates to `1` when processed right-to-left, while left-to-right would yield `-1`. The conversation also addresses the misconception of using the `!` operator in Python, emphasizing that it does not exist; the correct operator is `not`, which simply negates a boolean value. Additionally, it highlights that using reserved keywords like `not` as variable names is discouraged in Python to avoid confusion. Overall, the thread aims to clarify these concepts for better understanding of Python's syntax and operations.
Mr Davis 97
Messages
1,461
Reaction score
44
I have the following table for order of operations in Python. It all makes sense except for the unary operation negation. What does it mean that the order of precedence is from right to left? Can I have an example?Symbols - Operator - Type Order of precedence

( ) Parentheses Highest

- Unary (from right to left)

*, /, //, % Multiplicative (from left to right)

+, - Additive (from left to right)
 
Technology news on Phys.org
!false
means: false > not
which means true.

!false
means false > not > not
which means false
 
Mr Davis 97 said:
I have the following table for order of operations in Python. It all makes sense except for the unary operation negation. What does it mean that the order of precedence is from right to left? Can I have an example?Symbols - Operator - Type Order of precedence

( ) Parentheses Highest

- Unary (from right to left)

*, /, //, % Multiplicative (from left to right)

+, - Additive (from left to right)
The left to right or right to left business describes how an expression using a given operator associates, and is not related to the order of precedence. For example, the + additive operator associates left to right. This means that a + b + c is treated as if it had been written (a + b) + c.

The ~ bitwise "not"operator associates right to left. This means that the expression ~~x is evaluated as if it were written ~(~x). I didn't use the unary - for an example, because -- is a different operator (decrement).
 
Example -~0 is evaluated right-to-left as -(~0) = -(-1) = 1 whereas left to right it would be ~(-0) = ~0 = -1.
 
DaveC426913 said:
!false
means: false > not
which means true.
Not in python. There is no ! operator in python. The correct operator in python is no. And it doesn't mean false > not (whatever that means). not false in python means exactly what a naive reader would think it means, which is true.

!false
means false > not > not
which means false
Not in python. Here, a>b>c means exactly what a physicist or mathematician would read that to mean, as opposed to a computer scientist. In python, a>b>c means that b is between c (lower bound) and a (upper bound), exclusive of c and a.

Aside: Using tokens such as not in a non-reserved context is generally perceived as a bad thing in python. (A very bad thing; you will get yourself in deep trouble if you name a variable not or len.)
 
Last edited:
D H said:
Not in python. There is no ! operator in python.
Yeah. I prolly should have looked up the syntax of python before posting. :oops:
 
D H said:
There is no ! operator in python.The correct operator in python is no.
No it's not :biggrin:.
 
  • Like
Likes jim mcnamara
I see what you did there!
 
Yes, it's not.
 

Similar threads

Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 25 ·
Replies
25
Views
6K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K