Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Order of operations

  1. Sep 13, 2015 #1
    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)
     
  2. jcsd
  3. Sep 13, 2015 #2

    DaveC426913

    User Avatar
    Gold Member

    !false
    means: false > not
    which means true.

    !!false
    means false > not > not
    which means false
     
  4. Sep 13, 2015 #3

    Mark44

    Staff: Mentor

    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).
     
  5. Sep 13, 2015 #4
    Example -~0 is evaluated right-to-left as -(~0) = -(-1) = 1 whereas left to right it would be ~(-0) = ~0 = -1.
     
  6. Sep 13, 2015 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.

    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: Sep 13, 2015
  7. Sep 13, 2015 #6

    DaveC426913

    User Avatar
    Gold Member

    Yeah. I prolly should have looked up the syntax of python before posting. :oops:
     
  8. Sep 13, 2015 #7

    Mark44

    Staff: Mentor

    No it's not :biggrin:.
     
  9. Sep 13, 2015 #8

    DaveC426913

    User Avatar
    Gold Member

    I see what you did there!
     
  10. Sep 13, 2015 #9

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Yes, it's not.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Order of operations
  1. Modulus operators (Replies: 2)

Loading...