By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Logical AND question

P: n/a
Hello,
Can someone explain to me why only one of these two if/print statements
prints something? Thanks!
Louis

IDLE 1.0.2
s = ''
t = ''
if s and t == '': print "This doesn't work!"

s and t '' if s == '' and t == '': print "This does!"
This does!

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"3c273" <no****@nospam.com> writes:
if s and t == '':

print "This doesn't work!"


This is really

if (s) and (t == '')

which evaluates to false since "if (s)" returns false for the empty
string.
Jul 18 '05 #2

P: n/a
Tor Iver Wilhelmsen wrote:
"3c273" <no****@nospam.com> writes:
>>> if s and t == '':

print "This doesn't work!"


This is really

if (s) and (t == '')

which evaluates to false since "if (s)" returns false for the empty
string.


The original poster probably wants
if s == t == "": .... print "this works"
....
this works
or the conventional
if s == "" and t == "": .... print "but this is more common"
....
but this is more common


Peter

Jul 18 '05 #3

P: n/a
Thank you both :-)

"Peter Otten" <__*******@web.de> wrote in message
news:c8*************@news.t-online.com...
Tor Iver Wilhelmsen wrote:
"3c273" <no****@nospam.com> writes:
>>> if s and t == '':
print "This doesn't work!"


This is really

if (s) and (t == '')

which evaluates to false since "if (s)" returns false for the empty
string.


The original poster probably wants
if s == t == "": ... print "this works"
...
this works
or the conventional
if s == "" and t == "": ... print "but this is more common"
...
but this is more common


Peter

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.