Python Order of Operations: Unary Negation Explained

  • Context: Python 
  • Thread starter Thread starter Mr Davis 97
  • Start date Start date
  • Tags Tags
    Operations
Click For Summary

Discussion Overview

The discussion revolves around the order of operations in Python, specifically focusing on unary negation and its right-to-left precedence. Participants explore the implications of this precedence and clarify misconceptions regarding operators in Python.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions the meaning of right-to-left precedence for unary negation and requests examples.
  • Another participant explains that the order of precedence indicates how operators associate in expressions, using the additive operator as an example of left-to-right association.
  • A participant provides an example of how the expression -~0 is evaluated right-to-left, contrasting it with a left-to-right evaluation.
  • Some participants clarify that there is no ! operator in Python, emphasizing that the correct operator for negation is 'not'.
  • There is a discussion about the interpretation of expressions involving operators, with one participant asserting that the meaning of expressions can differ between programming and mathematical contexts.
  • Several participants acknowledge misunderstandings about Python syntax, particularly regarding the use of the ! operator.

Areas of Agreement / Disagreement

Participants generally agree that there is no ! operator in Python and that the correct operator for negation is 'not'. However, there is disagreement regarding the interpretation of expressions and the implications of operator precedence, with some participants providing conflicting explanations.

Contextual Notes

Some statements made by participants reflect misunderstandings about Python syntax and operator functionality, which may lead to confusion in the discussion.

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   Reactions: 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
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K