Menu Close

Python bool() Function

python bool function

In this python built-in functions tutorial series, we are going to learn all about the Python bool function to get the boolean value of the specified object.
In the previous Python tutorial, we have seen another Python built0 function that is a bin function to get the binary value of a given integer.

What is the Python bool() function?

The bool function is the part of Python built-in functions that are used to return the boolean value of passed objects to the bool() function. If no parameter is passed then it will return False always.

The bool() function will always return True, unless

  • The object is an empty list, set, tuple, and dictionary.
  • The object is None
  • The Object is 0
  • The object is False

Syntax

The syntax of the bool function in Python is:-

bool( object )

The parameter Value

bool function in python accepts only one parameter which is an object.

  • object:- Optional, Any object, like String, List, Number, etc.

Return Value

The return value of the bool function is either True or False.

Python bool function example

Let’s understand the bool function with different-different examples so that you can use it in your code without any hesitation.

Example: Passing integer value to bool()

Find the boolean value of 1 using the bool() function.


x=bool(1)
print(x)

The output will be:- True

Example: Passing zero value to bool()

Find the boolean value of 0 using the bool() function.


y=0
print(bool(y))

The output will be:- False

Example: Passing False value to bool()


data = False
print(bool(data))

The output will be:- False

Example: Passing empty and no-empty lists to bool()


print(bool([]))
print(bool(['Python', 'Django', 'Data Science']))

The Output will be:- False and True

Example: Passing set into bool()


print(bool({}))
print(bool({'1', '2', '3'}))

The output will be:- False and True

Example: Passing dictionary to bool()


print(bool({}))
print(bool({"name": "Programming Funda", "website": "www.programmingfunda.com"}))

The output will be again False and True.

Conclusion

The bool() function is a built-in function that is used to get the boolean value of the specified object. bool function in Python is very useful to find the boolean value of the specified object.

if you like this article, please keep visiting for the next Python built-in functions tutorials.

For more information:- Click Here

Thanks for your valuable time…

Python issubclass() Function

Related Posts