Booleans in Python
In this lesson, you will learn about Booleans in python and some of the Boolean functions in python and how to use them in your programs.
Booleans in Python
If you need to find whether the given condition is true or false, Booleans are go to choice in any programming language. Python Boolean is a built-in data type, consisting of two values i.e. True or False. Generally.
if 1==0: print(True) else: print (False)
bool() function in Python
bool() function in python is an inbuilt function that can be used to check the conditions. Using the bool() function, we can evaluate values and variables.
Instead of True and False, you can also use integers and Floats as Booleans. The bool() function, interprets zero as False and any other value including negative integers as True.
Boolean operations can be performed using the following operators.
- == (equivalent)
- != (not equivalent)
The ‘or’ operator in boolean returns True if any one of the inputs is True else returns False.
The ‘and’ operator in Boolean returns False if any one of the inputs is False else returns True.
The NOT operators in Boolean returns the opposite value. i.e. if the condition is true it returns false and vice versa.
Now let us understand the Boolean concept with the help of a simple program using the bool() function in python.
[gamipress_button type=”submit” label=”Run Code Snippet” onclick=”window.open(‘https://coderseditor.com/?id=1189′,’_blank’)”]
a=int(input("Enter the value of a:")) b=int(input("Enter the value of b:")) print(bool(a==b)) print(bool(a!=b)) print(bool(a or b)) print(bool(a and b))
Enter the value of a:23 Enter the value of b:32 False True True True
Here the result for a or b and a and b are true because Boolean takes an integer value as True and zero as false.
Learn more about python programming and other programming languages at DeveloperPublish.com