bool() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

bool() in Python is an inbuilt function in python that is used to return or convert a particular value or expression to the boolean value, i.e True and False.

Example:

bool(3232 == 2323) will result in false because the expression 32==2332==23 is not true, and hence the expression will result in false output.

What is the bool() in Python?

In Python, bool() is a built-in function designed to seamlessly transform various data types into boolean data types, such as strings, integers, and floats. Booleans are binary, capable of holding only two values: True or False.

When employing bool(), specific values are automatically interpreted as False, including 0, NULL, and empty containers like lists, tuples, and dictionaries. Conversely, any other value not falling under these conditions is considered True.

This function streamlines converting different data types into boolean values, facilitating logical operations and conditional expressions within Python code.

Syntax of bool() in Python

Parameters of bool() in Python

It takes one parameter, which is an optional object. If we don't pass any object to the bool function, the function will return false.

Return Values of bool() in Python

The bool in Python will return true or false according to the condition passed in the bool function, but in some cases, the bool in Python will return false. Let's understand the cases in which the bool method returns false.

  • if we don't pass any parameter in the bool function, the bool function will return false.
  • The return type is false if we pass a false boolean value to the bool() in python.
  • The bool function will return false if we pass an empty list or tuple as a parameter.
  • if we pass an empty dictionary object as a parameter, the bool() function in python will return false.
  • if we pass 0 as a parameter in the bool function, the bool() function in python will be false, and in the case of 1, it will return true.
  • When objects of classes that implement either the bool() or len() method return 0 or False.

Return Values of bool() in Python

How to Use bool() in Python?

Different values of different data types, and we are printing the return value of the bool() function in python.

Let's understand the bool() return type on different data types.

  • Let's understand the return type of bool() method for complex numbers.

Output:

The bool() method returns True for the complex numbers.

  • Let's understand the return type of bool() method for the empty String.

The bool() method returns false for an empty string as a parameter.

Output:

  • Let's understand the return type of bool() method for the empty tuple.

The bool() method returns False when an empty tuple is passed as a parameter.

Output:

User Input Boolean in Python

Here, we will take input as a parameter in the bool function and evaluate whether it is true or false.

Example:

Output

Find out Even and Odd by the use of bool() in python Method

Let's understand the bool function using an example:

In the example below, we are taking a number input from the user and checking whether the number is even or odd, and returning a bool value true if the number is odd; otherwise, the bool method returns false.

Output:

The 4 is an even number. That's why, as a parameter in the bool function, we get 0, and we know that the bool function returns false for the 0 parameter.

Exceptions of bool() in Python

The bool() in python method can throw many exceptions like ValueError, SyntaxError etc. The valueError is caused due to the wrong type of the arguments , and the syntaxerror is caused due to the use of invalid syntax.

We can handle these exceptions using try and Except block in the bool method.

Let's understand how to handle the valueError in the bool method:

Output:

In the bool() in python method, we are comparing the String and the number; this will cause an ValueError exception. To handle this exception, we use the except block; that's why the "This is not a number" is printed that is present in the block of except.

Conclusion

  • The bool() in python returns two values, either true or false.
  • The bool() function in python takes one optional parameter.
  • False is returned if 0 is passed as a parameter in the bool function.