Home » Python

Python operator.truth() Function with Examples

Python operator.truth() Function: Here, we are going to learn about the operator.truth() function with examples in Python programming language.
Submitted by IncludeHelp, on April 14, 2020

operator.truth() Function

operator.truth() function is a library function of operator module, it is used to check whether the given value is a true/truthy value or not, it returns True if the given value is a true/truthy value, False, otherwise.

Module:

    import operator

Syntax:

    operator.truth(x)

Parameter(s):

  • x – value to be checked true/truthy.

Return value:

The return type of this method is bool, it returns True if x is a true/truthy value, False, otherwise.

Example:

# 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()

Output:

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


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.