freeCodeCamp/guide/english/python/bool-function/index.md

30 lines
1.5 KiB
Markdown
Raw Normal View History

2018-10-12 19:37:13 +00:00
---
title: Python Bool Function
---
`bool()` is a built-in function in Python 3\. This function returns a Boolean value, i.e. True or False. It takes one argument, `x`.
## Arguments
It takes one argument, `x`. `x` is converted using the standard <a href='https://docs.python.org/3/library/stdtypes.html#truth' target='_blank' rel='nofollow'>Truth Testing Procedure</a>.
## Return Value
If `x` is false or omitted, this returns `False`; otherwise it returns `True`.
## Code Sample
print(bool(4 > 2)) # Returns True as 4 is greater than 2
print(bool(4 < 2)) # Returns False as 4 is not less than 2
print(bool(4 == 4)) # Returns True as 4 is equal to 4
print(bool(4 != 4)) # Returns False as 4 is equal to 4 so inequality doesn't holds
print(bool(4)) # Returns True as 4 is a non-zero value
print(bool(-4)) # Returns True as -4 is a non-zero value
print(bool(0)) # Returns False as it is a zero value
print(bool('dskl')) # Returns True as the string is a non-zero value
print(bool([1, 2, 3])) # Returns True as the list is a non-zero value
print(bool((2,3,4))) # Returns True as tuple is a non-zero value
print(bool([])) # Returns False as list is empty and equal to 0 according to truth value testing
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") <a href='https://repl.it/CVCS/2' target='_blank' rel='nofollow'>Run Code</a>
<a href='https://docs.python.org/3/library/functions.html#bool' target='_blank' rel='nofollow'>Official Docs</a>