<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>

      Python有三元条件运算符吗?

      时间:2024-04-21

        <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'
                >>> 'true' if False else 'false'
                'false'
                

                请注意,条件是表达式,而不是语句.这意味着您不能在条件表达式中使用赋值语句或pass或其他语句:

                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 的?:"三元运算符?

                这篇关于Python有三元条件运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 Pandas 中遍历 DataFrame 中的行 下一篇:如何将 pandas DataFrame 中的列取消嵌套(分解)成多行

                相关文章

                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>

                    <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>