當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。