Equality operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • italy

    #1

    Equality operator

    Why doesn't this statement execute in Python:

    1 == not 0

    I get a syntax error, but I don't know why.

    Thanks,
    Adam Roan

  • Kent Johnson

    #2
    Re: Equality operator

    italy wrote:[color=blue]
    > Why doesn't this statement execute in Python:
    >
    > 1 == not 0
    >
    > I get a syntax error, but I don't know why.[/color]

    Because == has higher precedence than 'not', so you are asking for
    (1 == not) 0

    Try[color=blue][color=green][color=darkred]
    >>> 1 == (not 0)[/color][/color][/color]
    True

    Kent
    [color=blue]
    >
    > Thanks,
    > Adam Roan
    >[/color]

    Comment

    • Marek Kubica

      #3
      Re: Equality operator

      [color=blue]
      > Why doesn't this statement execute in Python:
      >
      > 1 == not 0
      >
      > I get a syntax error, but I don't know why.[/color]

      This does: 1 == (not 0)
      I presume Python treats it like

      1 (== not) 0

      Which is a SyntaxError

      greets,
      Marek

      Comment

      • Chris Grebeldinger

        #4
        Re: Equality operator

        "not" has a lower priority than non-Boolean operators, so not a == b is
        interpreted as not (a == b), and a == not b is a syntax error.



        Comment

        • Marc 'BlackJack' Rintsch

          #5
          Re: Equality operator

          In <1110055534.040 063.304150@f14g 2000cwb.googleg roups.com>, italy wrote:
          [color=blue]
          > Why doesn't this statement execute in Python:
          >
          > 1 == not 0
          >
          > I get a syntax error, but I don't know why.[/color]

          `==` has a higher precedence than `not` so Python interprets it as::

          (1 == not) 0

          This works::
          [color=blue][color=green][color=darkred]
          >>> 1 == (not 0)[/color][/color][/color]
          True

          Ciao,
          Marc 'BlackJack' Rintsch

          Comment

          • Anthony  Boyd

            #6
            Re: Equality operator


            italy wrote:[color=blue]
            > Why doesn't this statement execute in Python:
            >
            > 1 == not 0
            >
            > I get a syntax error, but I don't know why.
            >
            > Thanks,
            > Adam Roan[/color]

            Of course, you would normally want to use != to see if something is not
            equal to something else.

            1 != 0
            True

            Comment

            Working...