<small id='VIgvK'></small><noframes id='VIgvK'>

  1. <tfoot id='VIgvK'></tfoot>
      <bdo id='VIgvK'></bdo><ul id='VIgvK'></ul>
  2. <i id='VIgvK'><tr id='VIgvK'><dt id='VIgvK'><q id='VIgvK'><span id='VIgvK'><b id='VIgvK'><form id='VIgvK'><ins id='VIgvK'></ins><ul id='VIgvK'></ul><sub id='VIgvK'></sub></form><legend id='VIgvK'></legend><bdo id='VIgvK'><pre id='VIgvK'><center id='VIgvK'></center></pre></bdo></b><th id='VIgvK'></th></span></q></dt></tr></i><div id='VIgvK'><tfoot id='VIgvK'></tfoot><dl id='VIgvK'><fieldset id='VIgvK'></fieldset></dl></div>
  3. <legend id='VIgvK'><style id='VIgvK'><dir id='VIgvK'><q id='VIgvK'></q></dir></style></legend>


      Does Python have a ternary conditional operator?(Python有三元条件运算符吗?)

        <tfoot id='GcgQ4'></tfoot>
      • <small id='GcgQ4'></small><noframes id='GcgQ4'>

          <tbody id='GcgQ4'></tbody>
        <i id='GcgQ4'><tr id='GcgQ4'><dt id='GcgQ4'><q id='GcgQ4'><span id='GcgQ4'><b id='GcgQ4'><form id='GcgQ4'><ins id='GcgQ4'></ins><ul id='GcgQ4'></ul><sub id='GcgQ4'></sub></form><legend id='GcgQ4'></legend><bdo id='GcgQ4'><pre id='GcgQ4'><center id='GcgQ4'></center></pre></bdo></b><th id='GcgQ4'></th></span></q></dt></tr></i><div id='GcgQ4'><tfoot id='GcgQ4'></tfoot><dl id='GcgQ4'><fieldset id='GcgQ4'></fieldset></dl></div>

          <bdo id='GcgQ4'></bdo><ul id='GcgQ4'></ul>
          <legend id='GcgQ4'><style id='GcgQ4'><dir id='GcgQ4'><q id='GcgQ4'></q></dir></style></legend>
              • 本文介绍了Python有三元条件运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!


                如果 Python 没有三元条件运算符,是否可以使用其他语言结构来模拟一个?

                If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?


                是的,是 在 2.5 版中添加.表达式语法为:

                Yes, it was added in version 2.5. The expression syntax is:

                a if condition else b

                首先评估 condition,然后根据 a 或 b 中的一个://en.wikipedia.org/wiki/Boolean_data_type" rel="noreferrer" title="布尔数据类型">布尔 条件的值.如果 condition 计算结果为 True,则计算并返回 a 但忽略 b,否则当 >b 被评估并返回,但 a 被忽略.

                First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, or else when b is evaluated and returned but a is ignored.

                这允许短路,因为当 condition 为真时,仅评估 a 而根本不评估 b,但当 >condition 为 false,仅评估 b 而根本不评估 a.

                This allows short-circuiting because when condition is true only a is evaluated and b is not evaluated at all, but when condition is false only b is evaluated and a is not evaluated at all.


                >>> 'true' if True else 'false'
                >>> 'true' if False else 'false'


                Note that conditionals are an expression, not a statement. This means you can't use assignment statements or pass or other statements within a conditional expression:

                >>> pass if False else x = 3
                  File "<stdin>", line 1
                    pass if False else x = 3
                SyntaxError: invalid syntax


                You can, however, use conditional expressions to assign a variable like so:

                x = a if True else b


                Think of the conditional expression as switching between two values. It is very useful when you're in a 'one value or another' situation, it but doesn't do much else.

                如果需要使用语句,则必须使用普通的if 语句,而不是条件表达式.

                If you need to use statements, you have to use a normal if statement instead of a conditional expression.

                请记住,一些 Python 爱好者不赞成它有几个原因:

                Keep in mind that it's frowned upon by some Pythonistas for several reasons:

                • 参数的顺序与经典的条件不同?a : b 三元运算符来自许多其他语言(如 C、C++、Go、Perl、Ruby、Java、Javascript 等),当人们不熟悉 Python 的惊奇"时,可能会导致 bug.行为使用它(他们可能会颠倒参数顺序).
                • 有些人认为它笨拙",因为它与正常的思维流程背道而驰(先考虑条件,然后再考虑效果).
                • 风格原因.(虽然 'inline if' 可以真正 有用,并使您的脚本更简洁,但它确实使您的代码复杂化)
                • The order of the arguments is different from those of the classic condition ? a : b ternary operator from many other languages (such as C, C++, Go, Perl, Ruby, Java, Javascript, etc.), which may lead to bugs when people unfamiliar with Python's "surprising" behaviour use it (they may reverse the argument order).
                • Some find it "unwieldy", since it goes contrary to the normal flow of thought (thinking of the condition first and then the effects).
                • Stylistic reasons. (Although the 'inline if' can be really useful, and make your script more concise, it really does complicate your code)

                如果您无法记住顺序,请记住,当您大声朗读时,您(几乎)说出了您的意思.例如,x = 4 if b >;8 else 9 被大声读出,因为 如果 b 大于 8,x 将为 4,否则为 9.

                If you're having trouble remembering the order, then remember that when read aloud, you (almost) say what you mean. For example, x = 4 if b > 8 else 9 is read aloud as x will be 4 if b is greater than 8 otherwise 9.


                • 条件表达式
                • 是否有等价于 C 的?:"三元运算符?




                Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                How to break out of multiple loops?(如何打破多个循环?)
                How to put the legend out of the plot(如何将传说从情节中剔除)
                Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                  1. <small id='oXG3j'></small><noframes id='oXG3j'>

                    <i id='oXG3j'><tr id='oXG3j'><dt id='oXG3j'><q id='oXG3j'><span id='oXG3j'><b id='oXG3j'><form id='oXG3j'><ins id='oXG3j'></ins><ul id='oXG3j'></ul><sub id='oXG3j'></sub></form><legend id='oXG3j'></legend><bdo id='oXG3j'><pre id='oXG3j'><center id='oXG3j'></center></pre></bdo></b><th id='oXG3j'></th></span></q></dt></tr></i><div id='oXG3j'><tfoot id='oXG3j'></tfoot><dl id='oXG3j'><fieldset id='oXG3j'></fieldset></dl></div>

                        <tbody id='oXG3j'></tbody>
                        <bdo id='oXG3j'></bdo><ul id='oXG3j'></ul>

                        1. <legend id='oXG3j'><style id='oXG3j'><dir id='oXG3j'><q id='oXG3j'></q></dir></style></legend><tfoot id='oXG3j'></tfoot>