Skip to content

Bool function in Python

Bool function in Python

Bool function in Python helps us to find out boolean equivalent of any value or variable . There are two types of boolean equivalent in python . One is Truth equivalent or Truthy value and another is False equivalent or Falsy value .

Syntax of bool function in Python

bool(object)

Parameters of bool function in Python

object = object means any type of python object . It can be any number , string , list etc. .

Return type of bool function Python

It returns a boolean value (True or false) .

Bool function in Python Examples

Example 1: Bool function on integer number

print("bool(1) =",bool(1))
print("bool(0) =",bool(0))
bool(1) = True
bool(0) = False

In the above code we can see that boolean equivalent of 1 is True and boolean equivalent of 0 is False .

Now we will understand about Truthy value and Falsy value with code –

Falsy value in Python

The value or variable whose boolean equivalent in Python is False is known as Falsy value . Here is the list of all Falsy values in python –

  • Integer : 0
  • Float : 0.0
  • Complex : 0j
  • Empty string : "" or ''
  • Empty list : []
  • Empty tuple : ()
  • Empty dictionary : {}
  • Empty set : set()
  • Empty range : range(0)
  • None
  • False

Example : Check Falsy values using bool function

>>> bool(0)
False
>>> bool(0.0)
False
>>> bool(0j)
False
>>> bool("")
False
>>> bool([])
False
>>> bool(())
False
>>> bool({})
False
>>> bool(set())
False
>>> bool(range(0))
False
>>> bool(None)
False
>>> bool(False)
False

Truthy value in Python

The value or variable whose boolean equivalent in Python is True is known as Truthy value . All other value or variable other than falsy value is truthy value in python .

Example : Check Truthy values using bool function

>>> bool(6)
True
>>> bool(17.5)
True
>>> bool(8j)
True
>>> bool("Rajkumar")
True
>>> bool(["a","b"])
True
>>> bool(("x","y"))
True
>>> bool({"a":1,"b":2})
True
>>> bool({"c","d"})
True
>>> bool(range(1))
True
>>> bool(True)
True

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply