Python Programs Demonstrating
Comparison Operators
Comparison Operators - Example Programs
These programs demonstrate the use of comparison operators like == , != , > ,
< , >= , and <=
1. WAP in Python to check if two numbers are equal or
not.
In [4]: # create required variables and assign value
a = 10
b = 20
# check the condtition using equal operator and print the result
if a == b:
print("a and b are equal")
else:
print("a and b are not equal")
a and b are not equal
2. WAP in Python to check if a number is not equal to
another number.
In [6]: # create required variables and assign value
x = 15
y = 10
# check the condtition using not equal operator and print the result
if x != y:
print("x and y are not equal")
else:
print("x and y are equal")
x and y are not equal
3. WAP in Python to check if a person's age is above 18 or
not. If true then print "You are eligible for voting"
otherwise print "You are not eligible for voting".
In [8]: # create required variable and assign value
age = 21
# check the condtition using greater than operator and print the result
if age > 18:
print("You are eligible for voting")
else:
print("You are not eligible for voting")
You are eligible for voting
4. WAP in Python to check if a number is less than
another number.
In [10]: # create required variable and assign value
a = 5
b = 12
# check the condtition using less than operator and print the result
if a < b:
print("a is less than b")
else:
print("a is not less than b")
a is less than b
5. WAP in Python to check if a student has passed or not
in a particaular subject. (Assume 40 and above marks is
required for passing)
In [12]: # create required variable and assign value
marks = 45
# check the condtition using greater or equal to than operator and print the result
if marks >= 40:
print("Student has passed")
else:
print("Student has failed")
Student has passed
6. WAP in Python to compare ages of two people.
In [14]: # create required variable and assign value
age1 = 30
age2 = 30
# check the condtition using less than or equal to operator and print the result
if age1 <= age2:
print("Person 1 is younger than or the same age as Person 2")
else:
print("Person 1 is older than Person 2")
Person 1 is younger than or the same age as Person 2