Python Order of Operations: Unary Negation Explained

  • Python
  • Thread starter Mr Davis 97
  • Start date
  • Tags
    Operations
In summary, the conversation discusses the order of operations and precedence in Python, specifically focusing on the unary operation negation and the use of operators such as not and !. The order of precedence is from right to left, and an example is given using the ~ bitwise "not" operator. It is also mentioned that using tokens like not in a non-reserved context is not recommended in Python.
  • #1
Mr Davis 97
1,462
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
  • #2
!false
means: false > not
which means true.

!false
means false > not > not
which means false
 
  • #3
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).
 
  • #4
Example -~0 is evaluated right-to-left as -(~0) = -(-1) = 1 whereas left to right it would be ~(-0) = ~0 = -1.
 
  • #5
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:
  • #6
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:
 
  • #7
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
  • #8
I see what you did there!
 
  • #9
Yes, it's not.
 

FAQ: Python Order of Operations: Unary Negation Explained

1. What is the purpose of unary negation in Python?

Unary negation in Python is used to change the sign of a number from positive to negative or vice versa. It is denoted by the minus (-) symbol and is commonly used in mathematical operations.

2. What is the order of operations for unary negation in Python?

The order of operations for unary negation in Python follows the standard mathematical rules of PEMDAS (parentheses, exponents, multiplication/division, addition/subtraction). Unary negation is performed after parentheses and before exponents, multiplication, and division.

3. Can unary negation be used with other operators in Python?

Yes, unary negation can be used with other operators in Python, such as addition (+), subtraction (-), multiplication (*), and division (/). It is important to understand the order of operations to ensure the correct result.

4. How does Python handle multiple unary negation operators?

When there are multiple unary negation operators in an expression, Python follows the right-associative rule, meaning it starts from the right and works its way to the left. For example, in the expression --5, the first negation will change the sign of 5 to negative, and the second negation will change it back to positive, resulting in a final value of 5.

5. Can unary negation be used on non-numeric values in Python?

No, unary negation can only be used on numeric values in Python. If it is applied to a non-numeric value, such as a string or boolean, it will result in an error. This is because unary negation is a mathematical operation and can only be performed on numbers.

Similar threads

Replies
3
Views
1K
Replies
2
Views
951
Replies
9
Views
2K
Replies
15
Views
1K
Replies
14
Views
2K
Replies
3
Views
593
Replies
1
Views
1K
Replies
8
Views
1K
Back
Top