A logical operator is the one that compares one or more values and return Boolean true or false. Python has a set of logical operators that are used in conditionals and loops. The python programming language supports following logical operators:
- Relational operators
- Set membership operators
- Boolean operators
Relational Operators
Lets us list all the logical operators.
Operator | Meaning |
< | less than |
<= | less than and equal to |
> | greater than |
>= | greater than and equal to |
== | equality operator |
!= | not equal to |
The above is the list of python relational operators. A relational operator in python compares both
In the following examples, we will check equality of two numbers and print the results.
# Checking equality of two numbers print(10 == 10) print(23 == 12) print(45 != 56) # Checking equality of two variable containing numbers num1 = 200 num2 = 100 print(num1 == num2)
The program above will print following results.
= RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/Numeric-Equalilty.py True False True False >>>
The value of variable
In the example below, we will check the equality of two strings.
str1 = "IT Employee" str2 = "IT Employee" str3 = "It Employee" print(str1 == str2) print(str2 == str3)
The python code above compares each character of both strings at corresponding location. It is going to be True, if
- The length of the strings match
- Each of the characters match at each position starting with 0.
Output of the above code is given below.
= RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/Non-Numeric_Quality.py True False >>>
In the case,
# Check if number is grater than another number number1 = 35 number2 = 44 print(number1 < number2) # Check if string is greater than another string str1 = "Hello" str2 = "hello" print(str1 > str2)
In the above example,
A \hspace{3px}B \hspace{3px}C\hspace{3px} D \hspace{3px}E\hspace{3px} F \hspace{3px}G \hspace{3px}... \hspace{3px}a \hspace{3px} b \hspace{3px}c \hspace{3px}d \hspace{3px}e...
Since,
Set Membership Operators
The set membership operators check if a number belong to a particular set. If it belongs then return
Operators | Meaning |
in | does the item belong in the list |
not in | does the item not belong to the list |
In the example, we will check a list of fruits for
my_list = ['oranges','grapes','apple','banana'] print('apple' in my_list) print('mango' not in my_list)
The program checks for
= RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/Set_Membership_operators.py True True >>>
The output is true for both statements involving set operators.
Boolean Operators
The Boolean operators are for complex conditions, when there are multiple conditions that need to be checked before a block of code executes in python. Here is a list of Boolean operators.
Operators | Meaning |
and | logical AND |
or | logical OR |
not | logical NOT |
Logical AND – compares two Boolean values and if both of them are true then return
exp = 3 > 2 exp2 = 5 > 3 if exp and exp2: print("condition is true") else: print("condition is false")
The above program checks both
>>> == RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/Logical AND.py = condition is true >>>
Variable A | Variable B | Output |
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Logical OR – compares two Boolean values and if any one of them is
exp = 3 > 7 exp2 = 5 > 3 if exp or exp2: print("condition is true") else: print("condition is false")
The
== RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/logical OR.py == condition is true >>>
Truth table for the logical OR is given below.
Variable A | Variable B | Output |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Logical NOT
This operator is also known as
# The my_exp is false at the beginning my_exp = 233 > 555 # Convert my_exp into true if not(my_exp): print("This statement is true now!") print("Done execution")
The
== RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/logical NOT.py = This statement is true now! Done execution >>>
Let us look at the truth table for logical NOT.
Variable A | Output |
True | False |
False | True |
Note: since, there is only one variable, the number of row is