Python Logical Operators

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:

  1. Relational operators
  2. Set membership operators
  3. Boolean operators

Relational Operators

Lets us list all the logical operators.

OperatorMeaning
<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 numeric values and non-numeric values and return True or False.

Let us see few examples.

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 num1 does not match with num2, therefore, the output is False. The same is tested for other equality.

In python, you can also check the equality of a string which is a non-numeric value.

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, print(str1 == str2 ), both string length and characters are equal, therefore, the program returns True.

In the second case, print(str2 == str3) is different by one character, therefore, program returns False.

Let us now check the relational operator for greater than or less that another number.

# 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, print(number1 < number2) is true because 35 < 44.

But, print(str1 > str2) is false. You need to understand why str1 > str2 is false.

The different between str1 and str2 is letter “H” and “h” respectively. In python, letters are in following sequence.

A B C D E F G H ... a b c d e.. 

Since, “H” comes after “h”, therefore, “h” is greater than “H”. Also, consider another capital after “H” such as “J”. The letter “J” is also greater than “H” because it comes after “H”.

Set Membership Operators

The set membership operators check if a number belong to a particular set. If it belongs then return true, else return false.

Here is the list of set membership operators.

OperatorsMeaning
indoes the item belong in the list
not indoes the item not belong to the list

In the example, we will check a list of fruits for ‘apple’ and output our result.

my_list = ['oranges','grapes','apple','banana']
print('apple' in my_list)
print('mango' not in my_list)

The program checks for ‘apple’ in the list and also, checks if ‘mango’ is not in the list.

The output of the program is as follows.

= RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/Set_Membership_operators.py
True
True
>>> 

The output is true for both the statement 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.

OperatorsMeaning
andlogical AND
orlogical OR
not logical NOT

Logical AND – compares two Boolean values and if both of them are true then return true, otherwise, return false.

Here is an example.

exp = 3 > 2
exp2 = 5 > 3
if exp and exp2:
   print("condition is true")
else:
   print("condition is false")

The above program checks both exp and exp2. If both are true, only then it prints “condition is true”. Otherwise, it will prints “condition is false“. The output is given below.

>>> 
== RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/Logical AND.py =
condition is true
>>> 
Variable AVariable BOutput
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Logical OR – compares two Boolean values and if any one of them is true, it returns a true. Otherwise, false.

exp = 3 > 7
exp2 = 5 > 3
if exp or exp2:
   print("condition is true")
else:
   print("condition is false")

The exp is false, but the exp2 is true which is enough to make the condition true and it prints “condition is true” as output.

The output is given below.

== RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/logical OR.py ==
condition is true
>>> 

Truth table for the logical OR is given below.

Variable AVariable BOutput
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Logical NOT

This operator is also known as negation operator. it will negate any true statement into false and false statement into true.

Consider the following example.

# 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 if block will never execute unless not operator convert the condition into true.

The output is given below.

== 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 AOutput
TrueFalse
FalseTrue

Note: since, there is only one variable, the number of row is 21 = 2 , if variable is 2, then row = 22 = 4.

Skip to content