In the previous article, we have discussed Python Program for abs() Function
bool() Function in Python:
Using the standard truth testing procedure, the Python bool() function returns or converts a value to a Boolean value, i.e., True or False.
Syntax:
bool([value])
Parameters
In general, the bool() method takes only one parameter (value), to which the standard truth testing procedure can be applied. If no parameters are passed, it returns False by default. As a result, passing a parameter is optional.
Return Value:
bool() function can only return one of two values.
- If the parameter or value passed is True, it returns True.
- If the parameter or value passed is False, it returns False.
Here are some examples of when Python’s bool() method returns false. Except for these, all other values are True.
- If the value False is passed.
- If the value None is used.
- If an empty sequence, such as (), [], “, etc., is passed
- If any numeric type, such as 0, 0.0, etc., is passed as Zero,
- If an empty mapping, such as, is passed.
- Objects of Classes with __bool__() or __len()__ methods that return 0 or False
Examples:
Example1:
Input:
Given value = False
Output:
False
Example2:
Input:
Given value = 'btechgeeks'
Output:
True
Program for bool() Function in Python
Using Built-in Functions (Static Input)
Approach:
- Give the value as False and store it in a variable.
- Get the Boolean value of the given value using the bool() function and print the result.
- Give the value as True and store it in another variable.
- Get the Boolean value of the given value using the bool() function and print the result.
- Give the two numbers as static input and store them in two separate variables.
- Check if the given two numbers are equal using the bool() function and print the result.
- Give the value as None and store it in another variable.
- Get the Boolean value of the given value using the bool() function and print the result.
- Take an empty tuple and store it in another variable.
- Get the Boolean value of the given empty tuple using the bool() function and print the result.
- Take an empty set and store it in another variable.
- Get the Boolean value of the given empty set using the bool() function and print the result.
- Give the floating value as static input and store it in another variable.
- Get the Boolean value of the given value using the bool() function and print the result.
- Give the string as static input and store it in another variable.
- Get the Boolean value of the given string using the bool() function and print the result.
- The Exit of the Program.
Below is the implementation:
# Give the value as False and store it in a variable. p = False # Get the Boolean value of the given value using the bool() function # and print the result. print(bool(p)) # Give the value as True and store it in another variable. q = True # Get the Boolean value of the given value using the bool() function # and print the result. print(bool(q)) # Give the two numbers as static input and store them in two separate variables. a = 4 b = 4 # Check if the given two numbers are equal using the bool() function # and print the result. print(bool(a == b)) # Give the value as None and store it in another variable. s = None # Get the Boolean value of the given value using the bool() function and # print the result. print(bool(s)) # Take an empty tuple and store it in another variable. t = () # Get the Boolean value of the given empty tuple using the bool() function # and print the result. print(bool(t)) # Take an empty set and store it in another variable. v = {} # Get the Boolean value of the given empty set using the bool() function # and print the result. print(bool(v)) # Give the floating value as static input and store it in another variable. g = 0.0 # Get the Boolean value of the given value using the bool() function # and print the result. print(bool(g)) # Give the string as static input and store it in another variable. d = 'btechgeeks' # Get the Boolean value of the given string using the bool() function # and print the result. print(bool(d))
Output:
False True True False False False False True
Example for Even or Odd Program using bool() Function
Below is the implementation:
def Even_or_odd(gvn_numb): return(bool(gvn_numb % 2 == 0)) gvn_numb = 9 if(Even_or_odd(gvn_numb)): print("The given number is Even") else: print("The given number is Odd")
Output:
The given number is Odd
Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.