Boolean Operators
Boolean Operations in Python are simple arithmetic of True and False values. These values can be manipulated by the use of boolean operators which include AND, Or, and NOT. Common boolean operations are –
- or
- and
- not
- == (equivalent)
- != (not equivalent)
Boolean OR Operator
The Boolean or operator returns True if any one of the inputs is True else returns False.
A | B | A or B |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Python Boolean OR Operator
In the example, we have used a Python boolean with an if statement and OR operator that checks if a is greater than b or b is smaller than c and it returns True if any of the conditions are True (b<c in the above example).
Python3
# Python program to demonstrate # or operator a = 1 b = 2 c = 4 if a > b or b < c: print ( True ) else : print ( False ) if a or b or c: print ( "Atleast one number has boolean value as True" ) |
True Atleast one number has boolean value as True
Boolean And Operator
The Boolean operator returns False if any one of the inputs is False else returns True.
A | B | A and B |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Python Boolean And Operator
In the first part of the code, the overall expression a > b and b < c evaluates to False. Hence, the code will execute the else block and print False. Whereas in the second part, a is 0, conditions a and b, and c will evaluate to False because one of the variables (a) has a boolean value of False. Therefore, the code will execute the else block and print “At least one number has a boolean value as False”.
Python3
# Python program to demonstrate # and operator a = 0 b = 2 c = 4 if a > b and b<c: print ( True ) else : print ( False ) if a and b and c: print ( "All the numbers has boolean value as True" ) else : print ( "Atleast one number has boolean value as False" ) |
False Atleast one number has boolean value as False
Boolean Not Operator
The Boolean Not operator only requires one argument and returns the negation of the argument i.e. returns the True for False and False for True.
A | Not A |
---|---|
True | False |
False | True |
Python Boolean Not Operator
The code demonstrates that when the value of a is 0, it is considered falsy, and the code block inside the if statement is executed, printing the corresponding message.
Python3
# Python program to demonstrate # not operator a = 0 if not a: print ( "Boolean value of a is False" ) |
Boolean value of a is False
Boolean == (equivalent) and != (not equivalent) Operator
Both operators are used to compare two results. == (equivalent operator returns True if two results are equal and != (not equivalent operator returns True if the two results are not same.
Python Boolean == (equivalent) and != (not equivalent) Operator
The code assigns values to variables a and b and then uses conditional statements to check if a is equal to 0, if a is equal to b, and if a is not equal to b. It prints True for the first and third conditions.
Python3
# Python program to demonstrate # equivalent an not equivalent # operator a = 0 b = 1 if a = = 0 : print ( True ) if a = = b: print ( True ) if a ! = b: print ( True ) |
True True
Python is Operator
The is keyword is used to test whether two variables belong to the same object. The test will return True if the two objects are the same else it will return False even if the two objects are 100% equal.
Python is Operator
The code first assigns the value 10 to variables x and y. It then compares x and y using the “is” operator and prints True because they refer to the same object. Next, it assigns two separate lists to x and y. It then compares x and y using the “is” operator and prints False because the lists are different objects in memory.
Python3
# Python program to demonstrate # is keyword x = 10 y = 10 if x is y: print ( True ) else : print ( False ) x = [ "a" , "b" , "c" , "d" ] y = [ "a" , "b" , "c" , "d" ] print (x is y) |
True False
Python in Operator
in operator checks for the membership i.e. checks if the value is present in a list, tuple, range, string, etc.
Python in Operator
The code creates a list of animals and checks if the string “lion” is present in the list. If “lion” is found in the list, it prints “True”.
Python3
# Python program to demonstrate # in keyword # Create a list animals = [ "dog" , "lion" , "cat" ] # Check if lion in list or not if "lion" in animals: print ( True ) |
True
Python Boolean
Python boolean type is one of the built-in data types provided by Python, which represents one of the two values i.e. True or False. Generally, it is used to represent the truth values of the expressions.
Example
Input: 1==1
Output: True
Input: 2<1
Output: False