bool() in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

bool 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.

Syntax of bool() in Python

Parameters of bool() in Python

Syntax:

The bool function in the method takes one parameter that is an object that is optional, 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 pass in the bool function but in some cases, 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.

Let's understand using an example:

Output:

  • if we pass a false boolean value to the bool function, the return type is false.

Let's understand using an example.

Output:

  • The bool function will return false if we pass an empty list or tuple as a parameter.

Let's understand using an example.

Output:

  • if we pass an empty dictionary object as a parameter, the bool function will return false.

Let's understand using an example:

Output:

  • if we pass 0 as a parameter in the bool function the bool function will be false, and in the case of 1 it will return true.

Let's understand using an example.

Output:

Return Values of bool in Python

Exceptions of bool() in Python

The bool 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 in the bool method using try and Except block.

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

Output:

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

Example of bool() in Python

Let's understand the bool method using an example.

In the below example, we are comparing two values using the comparison operators, the outcomes will be true or false which means either the condition of comparison is true or not.

Output:

In the first printing statement, we are checking whether the 1 is greater than the 2, but the 1 is smaller than the 2 that's why false is returned. In the second case, True is returned because 2 is greater than 1.

What is bool() in Python?

The bool method is an inbuilt method that is used to return a boolean value i.e true and false. The return value depends upon the condition.

For example, if we pass a comparison condition i.e 1>0 in the bool method, the bool method will return true, because the condition is true.

More Examples

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

Let's understand the bool function using an example:

In the below example, 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 bool function we get 0, and we know that bool function returns false for 0 parameter.

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

Let's understand the bool() return type on different kinds of 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:

Conclusion

  • The bool function returns two values either true or false.
  • The bool function takes one parameter that is optional.
  • If 0 is passed as a parameter in the bool function, false is returned.