Home  >  Q&A  >  body text

python - 能否解释一下这种特别的 if 语句?

print 'False' if (False) else 'True'

感觉有点类似 C/C++ 中的三目运算符。我在教程中都没有看到过相关的介绍。这是什么语法?

PHP中文网PHP中文网2765 days ago318

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:29:47

    https://docs.python.org/2/reference/expressions.html#conditional-expressions

    This is the syntax, just understand it.

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 14:29:47

    print 'False' if (False) else 'True'

    That is, if (False) ? printf("False") : printf("True")
    in C, JAVA and other languages if A?B:C converted to python is B if A else C

    reply
    0
  • Cancelreply