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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.