Python条件评估

use*_*890 0 python conditional if-statement

我希望下面的代码能够正常工作,因为第一个条件是假的,但它通过 IndexError: string index out of range.我错过了什么?

 a = False
 sample_strign = 'test'
 if (a == True) & (sample_strign[7] == 's'):
        print('foo')
Run Code Online (Sandbox Code Playgroud)

Bil*_*lly 6

&是一个有点明智的运营商.如果希望解释器"短路"逻辑,请使用逻辑运算符and.

if a and (sample_strign[7] == 's'):
Run Code Online (Sandbox Code Playgroud)