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

时间:2011-10-18 15:23:02

标签: python if-statement

在JavaScript中,人们可以这样做:

if (integer > 3 && integer < 34){
    document.write("Something")
}

这在Python中是否可行?

6 个答案:

答案 0 :(得分:49)

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

if integer > 3 and integer < 34

Python也非常聪明,可以处理:

if 3 < integer < 34:
    # do your stuff

答案 1 :(得分:12)

Python使用以下单词替换常用的C样式布尔运算符(&&||!):andor和{{1分别。

所以你可以这样做:

not

使事情更具可读性。

答案 2 :(得分:10)

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

if (isLarge and isHappy) \
or (isSmall and not isBlue):
     pass

它非常适合Python的梳子格式

答案 3 :(得分:5)

if integer > 3 and integer < 34:
    # do work

答案 4 :(得分:3)

是这样的:

if 3 < integer < 34:
    pass

答案 5 :(得分:1)

是的,它是:

if integer > 3 and integer < 34:
   document.write("something")