Python “if X == Y and Z” syntax

前端 未结 7 2146
温柔的废话
温柔的废话 2020-12-06 17:10

Does this:

if key == \"name\" and item:

mean the same as this:

if key == \"name\" and if key == \"item\":

7条回答
  •  渐次进展
    2020-12-06 17:26

    No, you have to repeat the expression. It evaluates as 2 separate conditions, and checks if both are true -

    1. x == y
    2. z

    Check the Python documentation for a list of what is considered False in Python

    (However, interesting to note that, as opposed to other languages, the following:

    if 3 < x < 6
    

    is equivalent to

    if x > 3 and x < 6
    

    )

提交回复
热议问题