Comparison Operators

Comparison operators are also known as relational operators. They are used to compare two values and return a Boolean ‘true’ or ‘false’ value.

== operator

‘==’ tries to establish whether two values are equal or not. If the two values are equal then in returns a ‘true’ else it will return ‘false’.

a = 5;
b = 10;
c = 5;
print('a=5; b=10; c=5')
if (a == b):
    print ('a is equal to b')
else:
    print ('a is not equal to b')
if (a==c):
    print ('a is equal to c')
else:
    print ('a is not equal to c')
if (b==c):
    print ('b is equal to c')
else:
    print ('b is not equal to c')

Download the code Run the code

!= operator

‘!=’ is the opposite of ‘==’ operator. Therefore, if the two values are not equal then in returns a ‘true’ else it will return ‘false’.

a = 5;
b = 10;
c = 5;
print('a=5; b=10; c=5')
if (a != b):
    print ('a is not equal to b')
else:
    print ('a is equal to b')
if (a != c):
    print ('a is not equal to c')
else:
    print ('a is equal to c')
if (b != c):
    print ('b is not equal to c')
else:
    print ('b is equal to c')

Download the code Run the code

‘>’ operator

In mathematics ‘>’ symbol stands for greater than value and this is exactly what this operator tries to find out. If the value of the left operand is greater than the value of the right operand then it returns true else it will return false.

a = 5;
b = 10;
c = 5;
print('a=5; b=10; c=5')
if (a > b):
    print('a is greater than b')
else:
    print('a is not greater than b')
if (a > c):
    print('a is greater than c')
else:
    print('a is not greater than c')
if (b > c):
    print('b is greater than c')
else:
    print('b is not greater than c')

Download the code Run the code

‘<’ operator

‘<’ symbol stands for less than value and this is exactly what this operator tries to find out. If the value of the left operand is less than the value of the right operand then it returns true else it will return false.

a = 5;
b = 10;
c = 5;
print('a=5; b=10; c=5')
if (a < b):
    print('a is less than b')
else:
    print('a is not less than b')
if (a < c):
    print('a is less than c')
else:
    print('a is not less than c')
if (b < c):
    print('b is less than c')
else:
    print('b is not less than c')

Download the code Run the code

‘>=’ operator

‘>=’ stands for greater than or equal to. If the operand on the left side is greater than or equal to the operand on the right it will return true or else it will return false

a = 5;
b = 10;
c = 5;
print ('a=5; b=10; c=5')
if (a >= b):
    print('a either greater or equal to b')
else:
    print ('a is neither greater nor equal to b')
if(a >= c):
    print ('a either greater or equal to c')
else:
    print ('a is neither greater nor equal to c')
if (b >= c):
    print ('b either greater or equal to c')
else:
    print ('b is neither greater nor equal to c')

Download the code Run the code

‘<=’ operator

‘<=’ stands for less than or equal to. If the operand on the left side is less than or equal to the operand on the right it will return true or else it will return false

a = 5;
b = 10;
c = 5;
print('a=5; b=10; c=5')
if (a <= b):
    print ('a either less than or equal to b')
else:
    print ('a is neither less than nor equal to b')
if (a <= c):
    print ('a either less than or equal to c')
else:
    print ('a is neither less than nor equal to c')
if (b <= c):
    print ('b either less than or equal to c')
else:
    print ('b is neither less than nor equal to c')

Download the code Run the code

<<< Python Lists – Negative Indexing, Slicing, Stepping, Comparing, Max and MinAssignment Operator >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .