当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python operator.truth()用法及代码示例


operator.truth() 函数

operator.truth()函数是一个库函数operator模块,用于检查给定的值是否为真/真值,它返回True如果给定的值是真/真值,False,否则。

模块:

    import operator

用法:

    operator.truth(x)

参数:

  • x– 要检查的值是否为真/真。

返回值:

这个方法的返回类型是bool,它返回True如果x是一个真/真值,False,否则。

例:

# Python operator.truth() Function Example

import operator

print("operator.truth(True):", operator.truth(True))
print("operator.truth(False):", operator.truth(False))
print()

x = 1
print("x:", x)
print("operator.truth(x):", operator.truth(x))
print()

x = 10
print("x:", x)
print("operator.truth(x):", operator.truth(x))
print()

x = -10
print("x:", x)
print("operator.truth(x):", operator.truth(x))
print()

x = 0
print("x:", x)
print("operator.truth(x):", operator.truth(x))
print()

x = "Hello"
print("x:", x)
print("operator.truth(x):", operator.truth(x))
print()

x = ""
print("x:", x)
print("operator.truth(x):", operator.truth(x))
print()

输出:

operator.truth(True):True
operator.truth(False):False

x:1
operator.truth(x):True

x:10
operator.truth(x):True

x:-10
operator.truth(x):True

x:0
operator.truth(x):False

x:Hello
operator.truth(x):True

x:
operator.truth(x):False


相关用法


注:本文由纯净天空筛选整理自 Python operator.truth() Function with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。