The bool() method is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.
Syntax:
bool([x])
The bool() method in general takes only one parameter(here x), on which the standard truth testing procedure can be applied. If no parameter is passed, then by default it returns False. So, passing a parameter is optional. It can return one of the two values.
- It returns True if the parameter or value passed is True.
- It returns False if the parameter or value passed is False.
Here are few cases, in which Python’s bool() method returns false. Except these all other values return True.
- If a False value is passed.
- If None is passed.
- If an empty sequence is passed, such as (), [], ”, etc
- If Zero is passed in any numeric type, such as 0, 0.0 etc
- If an empty mapping is passed, such as {}.
- If Objects of Classes having __bool__() or __len()__ method, returning 0 or False
# Python program to illustrate # built-in method bool() # Returns False as x is False x = Falseprint(bool(x)) # Returns True as x is True x = Trueprint(bool(x)) # Returns False as x is not equal to y x = 5y = 10print(bool(x==y)) # Returns False as x is None x = Noneprint(bool(x)) # Returns False as x is an empty sequence x = () print(bool(x)) # Returns False as x is an emty mapping x = {} print(bool(x)) # Returns False as x is 0 x = 0.0print(bool(x)) # Returns True as x is a non empty string x = 'GeeksforGeeks'print(bool(x)) |
Output:
False True False False False False False True
Application
Here is a program to find out even and odd by the use of bool() method. You may use other inputs and check out the results.
Example:
# Python code to check whether a number # is even or odd using bool() def check(num): return(bool(num%2==0)) # Driver Code num = 8; if(check(num)): print("Even") else: print("Odd") |
Output:
Even
Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

