A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/bool-in-python/ below:

bool() in Python - GeeksforGeeks

bool() in Python

Last Updated : 21 Feb, 2025

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)
Syntax of bool() function

bool([x])

Parameters Return Value of bool()

The bool() function returns:

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:

Truth Values:

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:

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:

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))  

Explanation:

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")

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4