pythonでのandとorの使い方

2962 ワード

From 《dive into python》
pythonのandは左から右に式を計算し、すべての値が真である場合は最後の値を返し、偽がある場合は最初の偽値を返します.
or
も左から計算式があり、最初の値が真の値を返します.
IDLE 1.2.4
>>>'a'and'b'
'b'
>>>''and'b'
''
>>>'a'or'b'
'a'
>>>''or'b'
'b'
bool? a : b
>>> a ='first'
>>> b ='second'
>>>1and a or b # bool = true
'first'
>>>0and a or b # bool = false
'second'
>>> a =''
>>>1and a or b # a ,
'second'
>>>(1and[a]or[b])[0]# , [a] ,
''
>>>