bool() in Python Program

PythonServer Side ProgrammingProgramming

In this tutorial, we are going to learn about the bool() built-in function. bool() is used to return or convert a value to boolean [True or False]. bool() will take 0 or 1 argument.

Syntax

bool(x)

If the value x is True, then it returns True otherwise it returns False. If you don't pass any argument to the bool(), then it will return False.

Example

 Live Demo 

# True
print(bool(1))
# False
print(bool())
# False
print(bool(0))
# True
print(bool('tutorialspoint'))

Output

If you run the above program, you will get the following results.

True
False
False
True

bool() will True always except in the following cases.

  • If you pass any of the following values, then bool() will return False.

    • None, (), [], 0, 0.0, {}, '', False

Example

Let's try

 Live Demo

print(bool(None))
print(bool(()))
print(bool([]))
print(bool(0))
print(bool(0.0))
print(bool({}))
print(bool(''))
print(bool(False))

Output

If you run the above program, you will get the following results.

False
False
False
False
False
False
False
False

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

raja
Updated on 02-Jan-2020 07:13:13

Advertisements