compound conditional statements

B

beliavsky

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?
 
J

Jeff Epler

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
 
J

Jeremy Yallop

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.
 
S

Steve Holden

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top