Open In App

bool() in Python

Python bool() function is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure. 

Example






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

Output
True
False

What is the bool() Method in Python?

bool() is a built-in function of Python programming language. It is used to convert any other data type value (string, integer, float, etc) into a boolean data type.



boolean data type can store only 2 values: True and False.

False Values: 0, NULL, empty lists, tuples, dictionaries, etc.

True Values: All other values will return true.

bool() Method Syntax

bool([x])

Parameters

Return

It can return one of the two values. 

Here are a few cases, in which Python’s bool() method returns false. Except these all other values return True. 

How to Use bool() Function

Using the bool() function in Python is very easy. You just need to pass the value as a parameter and it will convert it into a boolean data type.

Let’s understand better how to convert values into boolean data type with a simple example:

More Examples of bool() function

Let’s look at some of the examples of bool() functions. We will also cover different programs on bool function in this section.

Python bool() with Different Datatypes

In this example, we are checking the bool() method of Python with multiple types of variables like Boolean, Integers, None, Tuple, Float, strings, and Dictionary.




# Python program to illustrate
# built-in method bool()
 
# Returns False as x is False
x = False
print(bool(x))
 
# Returns True as x is True
x = True
print(bool(x))
 
# Returns False as x is not equal to y
x = 5
y = 10
print(bool(x == y))
 
# Returns False as x is None
x = None
print(bool(x))
 
# Returns False as x is an empty sequence
x = ()
print(bool(x))
 
# Returns False as x is an empty mapping
x = {}
print(bool(x))
 
# Returns False as x is 0
x = 0.0
print(bool(x))
 
# Returns True as x is a non empty string
x = 'GeeksforGeeks'
print(bool(x))

Output: 

False
True
False
False
False
False
False
True

User Input Boolean in Python

Here we take input in boolean(True/False) in boolean type with bool() function and check whether it is returned true or false.




user_input = bool(input("Are you hungry? True or false: "))
if user_input == "True":
    print(" You need to eat some foods ")
else:
    print("Let's go for walk")

Output:

Are you hungry? True or false: False
Let's go for walk

Python bool() function to check odd and even number

Here is a program to find out even and odd by the use of the bool() method. You may use other inputs and check out the results. 




# Python code to check whether a number
# is even or odd using bool()
 
def check(num):
    return(bool(num % 2 == 0))
 
# Driver Code
num = 8
if(check(num)):
    print("Even")
else:
    print("Odd")

Output: 

Even

We have covered the definition, syntax, uses, and examples of the bool() function in Python. bool() function is used in logical operations in programming like ‘and’, ‘or’, and ‘not’. It is also used in data validation, evaluating truthiness, conditional statements, etc.

Read Other Built-in Functions in Python

Similar Reads:


Article Tags :