Home » Python

bool() function with example in Python

Python bool() function: Here, we are going to learn about the bool() function in Python with example.
Submitted by IncludeHelp, on April 01, 2019

Python bool() function

bool() function 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, maps, 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:

    bool([value])

Parameter: 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: bool – a Boolean value

Example:

    Input:
    val = False
    print("val = ", bool(val))
    val = True 
    print("val = ", bool(val))
    val = 10
    print("val = ", bool(val))
    val = 0
    print("val = ", bool(val))

    Output:
    val =  False
    val =  True
    val =  True 
    val =  False

Python code to convert value to the Boolean value

# 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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.