---
# System prepended metadata

title: bool() in Python - Scaler Topics

---

---
canonical_url: https://www.scaler.com/topics/bool-in-python/
title: bool() in Python - Scaler Topics
description: 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.
author: Aayush Saini
category: Python
amphtml: https://www.scaler.com/topics/bool-in-python/amp/
publish_date: 2022-04-18
---
:::section{.abstract}

`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**.

**Brief example:**
 bool(32 == 23) will result in false because the expression 32==23 is not true, and hence the expression will result in false output. 
:::

:::section{.main}

## 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.
:::

:::section{.main}
## Syntax of bool() in Python   

``` python 
bool([x])
```
:::


:::section{.main}

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


:::
:::section{.main}

## 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](https://scaler.com/topics/images/return-values-of-bool-in-python.webp)
:::



:::section{.main}

## 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.**

```python
complexNumber=0+1j
print(bool(complexNumber))
```
**Output:**
```python
True
```
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.   

```python
String=""
print(bool(String))
```
**Output:**
```python
False
```
* **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.

```python
tuple1=()
print(bool(tuple1))
```
**Output:**
```python
False
```

### 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:** 
 
 ```python
# Prompt the user for their response and convert it to a boolean value
user_input = input("Will it rain today? (Enter True or False): ").strip().lower() == "true"

# Check the user's input
if user_input:
    print("We should not play cricket today.")
else:
    print("Let's play cricket!")    
```

**Output**

```
Will it rain today? (Enter True or False): True
We should not play cricket today.
```


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


```python
##Taking input from the user
number=(int)(input())
## Passing the remainder in the bool function by taking the modulas by 2
value=bool(number%2)
print(value)
```
**Output:**
```python
4
False
```
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.   

:::section{.main}

## 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:   
```python
try:
 number=input("Enter a number \n")
 if(bool(number>0)):
     print(("This is number is greater than zero"))
except ValueError:
    print("This is not a number")     

    
```
**Output:**
```python
Enter a number 
fs 
This is not a number

```
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.
:::

:::section{.summary}

## 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.
:::



