Python 3:有些反转if语句(Python 3: some reversed if statement)

编程入门 行业动态 更新时间:2024-10-26 04:30:53
Python 3:有些反转if语句(Python 3: some reversed if statement)

在我的教科书中,我遇到了这个例子:

lower = (lambda x, y: x if x < y else y) print(lower ('bb','aa'))

到目前为止我用过

if_stmt ::= "if" expression ":" suite ( "elif" expression ":" suite )* ["else" ":" suite]

所以,这个'x if x <y else y'让我有点惊讶。 你可以指导我:在文档中哪里可以找到这种结构?

先谢谢你

In my textbook I came across this example:

lower = (lambda x, y: x if x < y else y) print(lower ('bb','aa'))

So far I used

if_stmt ::= "if" expression ":" suite ( "elif" expression ":" suite )* ["else" ":" suite]

So, this 'x if x < y else y' astonished me a bit. Could you direct me: where in the documentation can I find this construction?

Thank you in advance

最满意答案

所以它们被称为“条件表达式”,您可以在python 3文档中找到它们,可以在2.5文档的这一部分中找到更清晰的解释。 我对条件赋值使用了很多,比如:

level = 1 if logging else 0

So they are called "Conditional Expressions" and you can find them in the python 3 docs a longer clearer explanation can be found in this section of of the 2.5 docs. I use it a lot for conditional assignment like:

level = 1 if logging else 0

更多推荐

本文发布于:2023-07-29 20:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   Python   statement   reversed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!