Python provides 3 logic operators named AND, OR, NOT. These logic operators are provided by all major programming languages and the base of the computing. These logical operators are used to combine conditional statements. In this tutorial, we will learn and examine how these operators are used for different cases.
and Logic Operator
The "and" logic is used to check if all given statements are True. If all given statements are True the "and" logic returns True. If one of the given statements is False AND logic returns False. Let's make some examples to understand "and" logic operator better. As we can see below "and" logic operator can be used with multiple statements which can be more than 2.
# result is True result = True and True # result is False result = True and False # result is True result = True and True and True # result is False result = True and True and True and False
or Logic Operator
The "or" logic is used to check multiple statements and returns Tue if at least one of them is True. If all of them False the or logic return False. We can say that the or operator is very optimistic and try to return True by matching at least one True statement. Like and logic operator the or logic operator can be used two or more statements.
# result is True result = True or True # result is True result = True or False # result is False result = False and False and False # result is True result = False and False and True and False
not Logic Operator
The not logic operator is different from the and, or logic operators. The not logic operator is used to reverse given logic value into reverse. There are two logic values called True, False. So the not True is equal to False, not False is equal to True. Let's make some examples to better understand the not logic operator.
# result is False result = not True # result is True result = not False if( 18 is not 19): print("That is true 18 is not 19")