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
Python3
x = bool(1)
print(x)
y = bool()
print(y)
|
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
- x: Any object that you want to convert into a boolean data type.
Return
It can return one of the two values.
- It returns True if the parameter or value passed is True.
- It returns False if the parameter or value passed is False.
Here are a few cases, in which Python’s bool() method returns false. Except these all other values return True.
- If a False value is passed.
- If None is passed.
- If an empty sequence is passed, such as (), [], ”, etc.
- If Zero is passed in any numeric type, such as 0, 0.0, etc.
- If an empty mapping is passed, such as {}.
- If Objects of Classes having __bool__() or __len()__ method, returning 0 or False.
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.
Python3
x = False
print(bool(x))
x = True
print(bool(x))
x = 5
y = 10
print(bool(x == y))
x = None
print(bool(x))
x = ()
print(bool(x))
x = {}
print(bool(x))
x = 0.0
print(bool(x))
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.
Python3
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.
Python3
def check(num):
return(bool(num % 2 == 0))
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:
Similar Reads
Python Built in Functions
Python is the most popular programming language created by Guido van Rossum in 1991. It is used for system scripting, software development, and web development (server-side). Web applications can be developed on a server using Python. Workflows can be made with Python and other technologies. Databas
6 min read
abs() in Python
The Python abs() function return the absolute value. The absolute value of any number is always positive it removes the negative sign of a number in Python. Example: Input: -29Output: 29Python abs() Function SyntaxThe abs() function in Python has the following syntax: Syntax: abs(number) number: Int
3 min read
Python - all() function
The Python all() function returns true if all the elements of a given iterable (List, Dictionary, Tuple, set, etc.) are True otherwise it returns False. It also returns True if the iterable object is empty. Sometimes while working on some code if we want to ensure that user has not entered a False v
3 min read
Python any() function
Python any() function returns True if any of the elements of a given iterable( List, Dictionary, Tuple, set, etc) are True else it returns False. Example Input: [True, False, False]Output: True Input: [False, False, False]Output: FalsePython any() Function Syntaxany() function in Python has the foll
5 min read
ascii() in Python
In Python, we have ascii() function that returns a string containing a printable representation of an object. In this article, we will see about ascii() in Python. Python ascii() FunctionPython ascii() function returns a string containing a printable representation of an object and escapes the non-A
3 min read
bin() in Python
Python bin() function returns the binary string of a given integer. bin() function is used to convert integer to binary string. In this article, we will learn more about Python bin() function. Example In this example, we are using the bin() function to convert integer to binary string. C/C++ Code x
2 min read
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 C/C++ Code x = bool(1) print(x) y = bool() print(y) OutputTrue False What is the bool() Method in Python?bool() is a built-in function of Python pro
4 min read
Python bytes() method
bytes() method in Python is used to create a sequence of bytes. In this article, we will check How bytes() methods works in Python. [GFGTABS] Python a = "geeks" # UTF-8 encoding is used b = bytes(a, 'utf-8') print(b) [/GFGTABS]Outputb'geeks' Table of Content bytes() Method SyntaxUs
3 min read
chr() Function in Python
chr() function returns a string representing a character whose Unicode code point is the integer specified. chr() Example: C/C++ Code num = 97 print("ASCII Value of 97 is: ", chr(num)) OutputASCII Value of 97 is: a Python chr() Function Syntaxchr(num) Parametersnum: an Unicode code integer
3 min read
Python dict() Function
A dictionary is a mutable data structure i.e. the data in the dictionary can be modified. A dictionary is an indexed data structure i.e. the contents of a dictionary can be accessed by using indexes, here in the dictionary, the key is used as an index. Here, the dict() function is used to create a n
4 min read