你能在Python中创建多个"if"条件吗?

ple*_* me 18 python if-statement

在JavaScript中,可以这样做:

if (integer > 3 && integer < 34){
    document.write("Something")
}
Run Code Online (Sandbox Code Playgroud)

这在Python中可行吗?

Nic*_*ico 52

Python确实允许你做这样的事情

if integer > 3 and integer < 34
Run Code Online (Sandbox Code Playgroud)

Python也很聪明,可以处理:

if 3 < integer < 34:
    # do your stuff
Run Code Online (Sandbox Code Playgroud)


and*_*kus 12

蟒取代通常的C风格布尔操作符(&&,||,!)与词:and,or,和not分别.

所以你可以这样做:

if (isLarge and isHappy) or (isSmall and not isBlue):
Run Code Online (Sandbox Code Playgroud)

这使事情更具可读性.


小智 10

只是格式化.如果你有很长的条件,我喜欢这种格式化方式

if (isLarge and isHappy) \
or (isSmall and not isBlue):
     pass
Run Code Online (Sandbox Code Playgroud)

它很适合Python的梳子格式

  • 使用\可能很危险.我将条件包裹在一组外围的parens中 (3认同)

tas*_*oor 5

if integer > 3 and integer < 34:
    # do work
Run Code Online (Sandbox Code Playgroud)