# Python Comparison Operators Example

These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.

## Example:

#!/usr/bin/python

a = 21

b = 10

c = 0

if ( a == b ):

print “Line 1 – a is equal to b”

else:

print “Line 1 – a is not equal to b”

if ( a != b ):

print “Line 2 – a is not equal to b”

else:

print “Line 2 – a is equal to b”

if ( a <> b ):

print “Line 3 – a is not equal to b”

else:

print “Line 3 – a is equal to b”

if ( a < b ):

print “Line 4 – a is less than b”

else:

print “Line 4 – a is not less than b”

if ( a > b ):

print “Line 5 – a is greater than b”

else:

print “Line 5 – a is not greater than b”

a = 5;

b = 20;

if ( a <= b ):

print “Line 6 – a is either less than or equal to  b”

else:

print “Line 6 – a is neither less than nor equal to  b”

if ( b >= a ):

print “Line 7 – b is either greater than  or equal to b”

else:

print “Line 7 – b is neither greater than  nor equal to b”

## Output:

Line 1 – a is not equal to b

Line 2 – a is not equal to b

Line 3 – a is not equal to b

Line 4 – a is not less than b

Line 5 – a is greater than b

Line 6 – a is either less than or equal to b

Line 7 – b is either greater than or equal to b

## Related Posts

• 84
There are used to compare the memory locations of two operators. Example: #!/usr/bin/python a = 20 b = 20 if ( a is b ): print "Line 1 - a and b have same identity" else: print "Line 1 - a and b do not have same identity" if (…
Tags: print, python, example, operators, pythonexample, operator, learn
• 66
Example: !/usr/bin/python a = 21 b = 10 c = 0 c = a + b print "Line 1 - Value of c is ", c c += a print "Line 2 - Value of c is ", c c *= a print "Line 3 - Value of c is…
Tags: print, example, python, operator, operators
• 66
Example: #!/usr/bin/python a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011…
Tags: print, example, operators, python, operator
• 65
Example: #!/usr/bin/python a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print "Line 1 - a is available in the given list" else: print "Line 1 - a is not available in the given list" if ( b not…
Tags: print, python, operators, example, learn
• 64
Example:  #!/usr/bin/python x = True y = False print('x and y is',x and y) print('x or y is',x or y) print('not x is',not x) Output: x and y is False x or y is True not x is False Few more example: 1. if (five == 5) AND (two ==…
Tags: example, print, operators, python, learn

#### Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.