compound conditional statements

Discussion in 'Python' started by beliavsky@aol.com, Sep 15, 2003.

  1. Guest

    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?
     
    , Sep 15, 2003
    #1
    1. Advertising

  2. Jeff Epler Guest

    On Mon, Sep 15, 2003 at 02:31:06PM -0700, wrote:
    > 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?


    Yes.

    For more information, read the language reference:
    http://python.org/doc/ref/Booleans.html
    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
     
    Jeff Epler, Sep 15, 2003
    #2
    1. Advertising

  3. wrote:
    > If I have code
    >
    > if (aa or bb): print "true",
    >
    > does Python evaluate aa and bb in any particular order?


    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.

    > Can I assume that if aa is true, bb will not be evaluated?


    Yes.

    Jeremy.
     
    Jeremy Yallop, Sep 15, 2003
    #3
  4. Steve Holden Guest

    <> wrote ...
    > 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?


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

    regards
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/pwp/
     
    Steve Holden, Sep 16, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fredrik Tolf

    Compound statements in expressions

    Fredrik Tolf, Sep 30, 2004, in forum: C Programming
    Replies:
    18
    Views:
    525
    Fredrik Tolf
    Sep 30, 2004
  2. Neil Cerutti

    Nested compound statements.

    Neil Cerutti, Feb 1, 2008, in forum: Python
    Replies:
    0
    Views:
    349
    Neil Cerutti
    Feb 1, 2008
  3. Kurt Euler
    Replies:
    1
    Views:
    178
    Jason Creighton
    Sep 3, 2003
  4. Randy Kramer
    Replies:
    11
    Views:
    386
    Randy Kramer
    Oct 19, 2007
  5. Bob

    compound conditional

    Bob, Apr 24, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    139
    Joe Smith
    Apr 25, 2005
Loading...

Share This Page