Open In App

bool() in Python

Last Updated : 21 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, bool() is a built-in function that is used to convert a value to a Boolean (i.e., True or False). The Boolean data type represents truth values and is a fundamental concept in programming, often used in conditional statements, loops and logical operations.

bool() function evaluates the truthness or falseness of a given value and returns either True or False. Understanding how bool() works is crucial for writing effective and efficient Python code.

Example:

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

Output
True
False

Syntax of bool() function

bool([x])

Parameters

  • x: represents the value that we want to convert to a Boolean. If no argument is provided, bool() returns False by default.

Return Value of bool()

The bool() function returns:

  • True if the value x is considered "truth"
  • False if the value x is considered "false."

Truth and False Values in Python

In Python, certain values are considered "truth" or "false" based on their inherent properties. Here’s a breakdown:

False Values:

  • False: The Boolean value False.
  • None: The None keyword, which represents the absence of a value.
  • 0: The integer zero.
  • 0.0: The floating-point zero.
  • "": An empty string.
  • []: An empty list.
  • (): An empty tuple.
  • {}: An empty dictionary.
  • set(): An empty set.
  • range(0): An empty range.

Truth Values:

  • Non-zero numbers (e.g., 1-13.14).
  • Non-empty strings (e.g., "hello").
  • Non-empty collections (e.g., [1, 2, 3]{"key": "value"}).
  • Objects (e.g., instances of classes).

Examples of bool()

Example 1: Basic usage

Python
print(bool(True))    
print(bool(False))   
print(bool(0))       
print(bool(1))       
print(bool(-1))      
print(bool(0.0))     
print(bool(3.14))    
print(bool(""))      
print(bool("Hello")) 
print(bool([]))      
print(bool([1, 2]))  
print(bool({}))      
print(bool({"a": 1}))
print(bool(None))    

Output
True
False
False
True
True
False
True
False
True
False
True
False
True
False

Explanation:

  • bool(True) returns True because True is already a Boolean value.
  • bool(False) returns False because False is already a Boolean value.
  • bool(0) returns False because 0 is a false value.
  • bool(1) returns True because 1 is a truth value.
  • bool(-1) returns True because any non-zero number is truth.
  • bool(0.0) returns False because 0.0 is a false value.
  • bool(3.14) returns True because 3.14 is a non-zero number.
  • bool("") returns False because an empty string is false.
  • bool("Hello") returns True because a non-empty string is truth.
  • bool([]) returns False because an empty list is false.
  • bool([1, 2]) returns True because a non-empty list is truth.
  • bool({}) returns False because an empty dictionary is false.
  • bool({"a": 1}) returns True because a non-empty dictionary is truth.
  • bool(None) returns False because None is a false value.

Example 2: Using bool() in conditional statement

Python
value = 10

if bool(value):
    print("The value is truth.")
else:
    print("The value is false.")

Output
The value is truth.

Explanation:

  • The bool(value) function is used to check if value is truth or false.
  • Since value is 10, which is a non-zero number, bool(value) returns True.
  • Therefore, the if block is executed, and the message "The value is truth." is printed.

Example 3: Using bool() with custom objects

Python
class gfg:
    def __init__(self, value):
        self.value = value

    def __bool__(self):
        return bool(self.value)

obj1 = gfg(0)
obj2 = gfg(42)

print(bool(obj1))  
print(bool(obj2))  

Output
False
True

Explanation:

  • In this example, we define a custom class gfg with an __init__ method and a __bool__ method.
  • The __bool__ method is a special method in Python that defines the truthiness of an object when bool() is called on it.
  • When bool(obj1) is called, the __bool__ method of obj1 is invoked, which returns bool(self.value). Since self.value is 0bool(obj1) returns False.
  • Similarly, bool(obj2) returns True because self.value is 42, which is a non-zero number.

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
def check_even(num):
    return bool(num % 2 == 0)

num = 8

if check_even(num):
    print("Even")
else:
    print("Odd")

Output
Even

Explanation:

  1. num % 2 == 0 this checks if the number is divisible by 2.
  2. bool(num % 2 == 0) converts the result into True (even) or False (odd).
  3. conditional check if True, prints "Even" otherwise "Odd".

Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy