Friday, December 27, 2024
Google search engine
HomeLanguagesbool() in Python

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

Python3




x = bool(1)
print(x)


Output:

True

Python bool() Syntax

Syntax: bool([x])

bool() parameters

The bool() method in general takes only one parameter(here x), on which the standard truth testing procedure can be applied. If no parameter is passed, then by default it returns False. So, passing a parameter is optional. 

The return value from bool()

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.

bool() function in Python Examples

Python bool() with Different Datatypes

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

Python3




# 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 = 'Lazyroar'
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




# 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

Certainly! If you’re interested in learning more about Boolean values in Python, you might find the following article on Lazyroar helpful: Boolean Data Type in Python. This article covers the basics of Boolean values in Python, as well as some advanced topics such as Boolean operators and truth tables. It also provides several examples to help you understand how Boolean values work in practice. Check it out if you’d like to learn more!

RELATED ARTICLES

Most Popular

Recent Comments