×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python bool() Function: Use, Syntax, and Examples

By IncludeHelp Last updated : December 07, 2024

Python bool() function

The bool() function is a library function in Python, it is used to convert a given value to the Boolean value (True or False) as per the standard truth testing procedures. It accepts a value (like an integer, list, map, etc) and converts it into a Boolean value.

Some of the examples:

  • None – converts to False
  • False – converts to False
  • Zeros (0, 0.0, 0j) – converts to False
  • Empty sequences like, (), [], ' ' – converts to False

Syntax

The following is the syntax of bool() function:

bool([value])

Parameter(s):

The following are the parameter(s):

  • value – A value to be converted to the Boolean value, it's an optional parameter, if we do not pass any parameter – it returns False.

Return Value

The return type of bool() function is <class 'bool'>, it returns a Boolean value either True or False.

Python bool() Function: Example 1

# python code to demonstrate example
# of bool() function

x = 10
y = True
z = False

print(bool(x))
print(bool(y))
print(bool(z))

Output

True
True
False

Python bool() Function: Example 2

# python code to demonstrate example
# of bool() function

val = False
print("val = ", bool(val))

val = True
print("val = ", bool(val))

val = 10
print("val = ", bool(val))

val = 0
print("val = ", bool(val))

val = 10.23
print("val = ", bool(val))

val = 0.0
print("val = ", bool(val))

val = "Hello"
print("val = ", bool(val))

val = []
print("val = ", bool(val))

Output

val =  False
val =  True
val =  True
val =  False
val =  True
val =  False
val =  True
val =  False


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.