×


Python bool() Function in Linux



In Python, the bool() function is used to convert a value to a boolean value (True or False) to check the truth according to the standard.

If the object has a value of None, False or equal to 0, the bool() function will return false. Except for all of the above, the bool() function will return true.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Python queries.

In this context, we shall look into how to use the bool() function in Python.


More about Python bool() Function

The bool() function can return 2 values: True and False.

If object is empty like (), [], {}, the function will return False.


What is the syntax of Python bool() Function ?

It is given below:

bool(object)

It's Parameter Values is:

object: any object. For example: number, string, list…


Examples of using Python bool() Function

1. Consider the below function:

x = []
print(bool(x))
y = 1
print(bool(y))

It's Output will look like this:

False
True


2. Basic bool() function:

x = 1
y = 2
print(bool(x == y))

The Output will be:

False


3. Parity check program:

def check(x):
return (bool(x % 2 == 0))
x = 4
if (check(x)):
print("Correct")
else:
print("Incorrect")

It's Output is:

Correct


[Need assistance in fixing Python problems ? We can help you. ]


Conclusion

This article covers how to use the bool() function in Python. In fact, The bool() method is a built-in Python method that applies the standard truth testing procedure to the passed object/value and returns a boolean value. Moreover, the bool class cannot be sub-classed. Its only instances are False and True.


More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com