• <small id='A6pOT'></small><noframes id='A6pOT'>

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

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

        Python 条件运算符解决方法如何工作?

        How does the Python conditional operator workaround work?(Python 条件运算符解决方法如何工作?)

        • <bdo id='7otqC'></bdo><ul id='7otqC'></ul>
              <tfoot id='7otqC'></tfoot>

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

                  <tbody id='7otqC'></tbody>

                <legend id='7otqC'><style id='7otqC'><dir id='7otqC'><q id='7otqC'></q></dir></style></legend>
                1. <small id='7otqC'></small><noframes id='7otqC'>

                  本文介绍了Python 条件运算符解决方法如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  根据我的阅读,我发现内置的三元运算符不存在(我很乐意了解更多信息.).

                  From what I have read, I found that a built-in ternary operator does not exist (I will be happy to know more about it.).

                  我找到了以下代码作为替代:

                  I found the following code as a substitute:

                  def val():
                      var = float(raw_input("Age:"))
                      status = ("Working","Retired")[var>65]
                      print "You should be:",status
                  

                  我无法理解这段代码是如何工作的;谁能解释一下代码实际上是如何工作的?我也很想知道为什么三元运算符不存在;任何有关此的参考或链接都会很有用.

                  I couldn't understand how this code works; can anyone explain me how actually the code is working? I am also interested to know why the ternary operator doesn't exist; any references or links about this will be ore useful.

                  我在 Windows Vista 上运行 Python 2.6.4.

                  I'm running Python 2.6.4 on Windows Vista.

                  推荐答案

                  Python 的构造有点像 C 中的三元运算符等.它的工作原理是这样的:

                  Python has a construct that is sort of like the ternary operator in C, et al. It works something like this:

                  my_var = "Retired" if age > 65 else "Working"
                  

                  并且等价于这个 C 代码:

                  and is equivalent to this C code:

                  my_var = age > 65 ? "Retired" : "Working";
                  

                  至于您发布的代码是如何工作的,让我们逐步介绍一下:

                  As for how the code you posted works, let's step through it:

                  ("Working","Retired")
                  

                  创建一个 2 元组(一个不可变列表),索引 0 为元素Working",索引 1 为Retired".

                  creates a 2-tuple (an immutable list) with the element "Working" at index 0, and "Retired" at index 1.

                  var>65
                  

                  如果 var 大于 65,则返回 True,否则返回 False.应用于索引时,它会转换为 1 (True) 或 0 (False).因此,这个布尔值提供了在同一行创建的元组的索引.

                  returns True if var is greater than 65, False if not. When applied to an index, it is converted into 1 (True) or 0 (False). Thus, this boolean value provides an index into the tuple created on the same line.

                  为什么 Python 不是一直都有三元运算符?简单的答案是 Python 的作者 Guido van Rossum 不喜欢/不想要它,显然认为它是一种不必要的构造,可能导致混淆代码(以及任何在C 可能会同意).但是对于 Python 2.5,他心软了,添加了上面看到的语法.

                  Why hasn't Python always had a ternary operator? The simple answer is that Guido van Rossum, the author of Python, didn't like/didn't want it, apparently believing that it was an unnecessary construct that could lead to confusing code (and anyone who's seen massively-nested ternary operators in C can probably agree). But for Python 2.5, he relented and added the grammar seen above.

                  这篇关于Python 条件运算符解决方法如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                      <tbody id='oDgNM'></tbody>
                    <legend id='oDgNM'><style id='oDgNM'><dir id='oDgNM'><q id='oDgNM'></q></dir></style></legend>

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

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