compound conditional statements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • beliavsky@aol.com

    compound conditional statements

    If I have code

    if (aa or bb): print "true",

    does Python evaluate aa and bb in any particular order? Can I assume that if
    aa is true, bb will not be evaluated?
  • Jeremy Yallop

    #2
    Re: compound conditional statements

    beliavsky@aol.c om wrote:[color=blue]
    > If I have code
    >
    > if (aa or bb): print "true",
    >
    > does Python evaluate aa and bb in any particular order?[/color]

    Yes. `aa' is evaluated first. If it is true, `bb' is not evaluated
    and the value of `aa' is the value of the expreesion. If `aa' is
    false, the value of `bb' is the value of the expression.
    [color=blue]
    > Can I assume that if aa is true, bb will not be evaluated?[/color]

    Yes.

    Jeremy.

    Comment

    • Jeff Epler

      #3
      Re: compound conditional statements

      On Mon, Sep 15, 2003 at 02:31:06PM -0700, beliavsky@aol.c om wrote:[color=blue]
      > If I have code
      >
      > if (aa or bb): print "true",
      >
      > does Python evaluate aa and bb in any particular order? Can I assume that if
      > aa is true, bb will not be evaluated?[/color]

      Yes.

      For more information, read the language reference:
      The official home of the Python Programming Language

      The expression x or y first evaluates x; if x is true, its value is
      returned; otherwise, y is evaluated and the resulting value is
      returned.

      Jeff

      Comment

      • Steve Holden

        #4
        Re: compound conditional statements

        <beliavsky@aol. com> wrote ...[color=blue]
        > If I have code
        >
        > if (aa or bb): print "true",
        >
        > does Python evaluate aa and bb in any particular order? Can I assume that[/color]
        if[color=blue]
        > aa is true, bb will not be evaluated?[/color]

        Yes. Both "and" and "or" are specifcally short-circuiting.

        regards
        --
        Steve Holden http://www.holdenweb.com/
        Python Web Programming http://pydish.holdenweb.com/pwp/



        Comment

        Working...