Logical Operators Python
After the arithmetic operators, we move on to the logical operators. These logical operators are used on boolean type when we require the output to be either true or false i.e. boolean values, however for non-boolean type the output will not always be boolean.
The logical operators are:-
- and– In this logical operator when both the arguments are true then the output is true.
- or– In this logical operator when either one of the arguments is true then the output is true otherwise false.
- not– It returns boolean values only. It in inverts the value given.
- For boolean type: can be very easily used with the boolean datatype.
Example:
>>>True and False #using and >>>True or False #using or >>>not True #using not
Output:
False #first output True #second output False #third output
In the above code, the first line has True and False with and operator and as we know that and operator returns true only when both the arguments are true therefore the output is false. In the second line we have true and false with or. Or operator returns true when any one of the arguments is true therefore the output is true. In the third line we have not of true which means that output will be inverted value of true that is false.
- For other types:
Applying the logical operators for datatypes other than boolean:
When we apply the logical operators on other datatypes then:
- Zero would mean False
- Any non-0 value means true
- An empty string (‘’) will return False
-
a and b:
If we are evaluating two variables a and b then,
- If a is evaluated as false then the output returns ‘a’ otherwise returns ‘b’.
Example:>>>20 and 30
Output:30
In the above example we are using the logical operators on two Numbers 20 and 30 and according to the and operator, it gives the first number as output when it is false that is zero. Here the first number is 20 which is not equal to 0, therefore, the output will be the second number that is 30.
Example:>>>0 and 30
Output:0
As we discussed in the example above that and operator returns the first value only when the first value is false that is zero and here the first argument is zero, therefore, the output is zero.
Example:>>>1 and ‘mew’
Output:‘mew’
In this example, we have a number and a string literal. Since the first argument is 1 which is not equal to 0, therefore, the output will be the second argument which is the string ‘mew’.
Example:>>>0 and ‘mew’
Output:0
Here again, we have a number and a string. Since the number is zero which means we get the output 0.
-
a or b:
- If a is evaluated as true then the output returns ‘a’ otherwise returns ‘b’.
Example:>>>20 or 30
Output:20
Here we have two numbers 20 and 30. The or logical operator returns the first value only when the first value is true. Here the number 20 is a non-0 number i.e. true hence the output is 20.
Example:>>>0 or 30
Output:30
In this example we have 0 and 30 with the or operator. Since the first argument is false therefore we get the second argument as output i.e. 30.
-
not a:
- When true is entered it returns false and vice versa.
Example:>>>not 20
Output:False
In the first example, we have not of 20. The not operator will invert the value of the given argument. Since any non-0 value is considered as true therefore the inverted value of 20 will be false.
Example:>>>not 0
Output:True
In the second example, we have not of 0. The not operator will invert the value of 0 i.e. false therefore the output will be true.
Example:>>>not ‘’
Output:True
In the above code, we have not to empty single quotes i.e. an empty string. Since the empty string is considered as False hence the output will be inverted value of False i.e. True.