繁体   English   中英

条件的Python模拟? 真假

[英]Python analog of condition? true : false

标准库中是否有python函数,例如

def cond(condition, true, false):
  if condition:
    return true
  return false

x = 20
s = cond(x > 10, "x greater than 10", "x less or equals 10")

Python具有三元运算,但它是作为“ if表达式”完成的,而不是带问号和冒号的。

s = "x greater than 10" if x > 10 else "x less or equals 10"

Python具有类似三元运算符(实际上称为条件表达式 ),其内容如下:

s = "x greater than 10" if x > 10 else "x less or equals 10"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM