5

and両方の条件が true の場合に true と評価されるブール値に論理的に使用されることはわかっていますが、次のステートメントに問題があります。

print "ashish" and "sahil"

it prints out "sahil"?
 another example:
 return s[0] == s[-1] and checker(s[1:-1])
 (taken from recursive function for palindrome string
 checking            
please explain it and other ways and is oveloaded ,especially what the second statement do.
4

3 に答える 3

10

and過負荷ではありません。

あなたのコードで"ashish"は、 は真の値です (空でない文字列は真であるため)。したがって、 と評価され"sahil"ます。As"sahil"も真の値で"sahil"あり、print ステートメントに返されてから出力されます。

于 2013-09-05T19:21:55.740 に答える
7

x and y基本的には次のことを意味します:

return 、 Falseっぽいものyでない限り- そのような場合は returnxx

可能な組み合わせのリストは次のとおりです。

>>> from itertools import combinations
>>> items = [True, False, 0, 1, 2, '', 'yes', 'no']
>>> for a, b in combinations(items, 2):
    print '%r and %r => %r' % (a, b, a and b)


True and False => False
True and 0 => 0
True and 1 => 1
True and 2 => 2
True and '' => ''
True and 'yes' => 'yes'
True and 'no' => 'no'
False and 0 => False
False and 1 => False
False and 2 => False
False and '' => False
False and 'yes' => False
False and 'no' => False
0 and 1 => 0
0 and 2 => 0
0 and '' => 0
0 and 'yes' => 0
0 and 'no' => 0
1 and 2 => 2
1 and '' => ''
1 and 'yes' => 'yes'
1 and 'no' => 'no'
2 and '' => ''
2 and 'yes' => 'yes'
2 and 'no' => 'no'
'' and 'yes' => ''
'' and 'no' => ''
'yes' and 'no' => 'no'
于 2013-09-05T19:21:49.710 に答える
4

の左側の式の結果andが偽の場合、そのように評価されます。それ以外の場合は、右側の式の結果に評価されます。"ashish"真実です。

于 2013-09-05T19:22:17.287 に答える