Python bool() FunctionThe bool() function is one of the built in methods in python which outputs the Boolean value depending on the input parameters. The Boolean values True or False are returned from the single argument. The python bool() method converts value to boolean (True or False) using the standard truth testing procedure. SyntaxParametersIt is not mandatory to pass value to bool(). If you do not pass the value, bool() returns False. In general , bool() takes a single parameter value. ReturnThe bool() returns:
Python bool() Function Example 1Code Output: [] is False [] is False [0] is True 0.0 is False None is False True is True Easy string is True Python bool() Function Example 2Using bool() function with if-else statements. Code Output: number is less than or equal to 15 In the above code, If the condition is true or false, then the respective block is executed. Now, lets see the explicit conversion of value into Boolean value in the below example. # Using bool() function to convert the condition to a boolean value Code Output: number is less than or equal to 15 Python bool() Function Example 31. Using bool() function to check if a value is True or False Code Output: True 2. To Check if an empty string is True or False Code Output: False 3. To Check if a zero value is True or False Code Output: False Conclusion:All in all, the bool() function is a useful asset in Python for working with boolean values. It very well may be utilized to switch any value over completely to a boolean value expressly, and it can likewise be utilized to check in the event that a value is true or false. Next TopicPython Built in Functions |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India