< Prev
Next >

# Python - Relational Operators

There are six relational operators in Python -
• <(less than operator)
• >(greater than operator)
• <=(less than equals to operator)
• >=(greater than equals to operator)
• ==(equals to operator)
• !=(not equals to operator)

## < (less than operator)

This operator is also known as less than operator. - It compares the values of two operands around it. < operator returns a boolean value true when operand on its left is less than the operand on its right, otherwise it returns false.

## A coding example with < operator

``````# Python < operator example

if 5 < 10:		#comparing an integer to another integer
print("5 is less than 10")

if 'a' < 'b':		#comparing a string with another string(in Python even a single character is termed as a string)
print("a is less than b")

if 100.5 < 100.4 :	#comparing a floating value with another floating value
print("100.5 is less than 100.4")

if 100 < 100.001 :	#comparing an integer with a floating value
print("100 is less than 100.001")
``````

## Output is

``````5 is less than 10
a is less than b
100 is less than 100.001``````

## > (greater than operator)

This operator is also known as greater than operator. - It compares the values of two operands around it. > operator returns a boolean value true when operand on its left is greater than the operand on its right, otherwise it returns false.

## A coding example with > operator

``````# Python > operator example

if 10 > 10 :		#comparing an integer with an integer
print("10 is greater than 10")

if 'b' > 'a' :		#comparing a string with a string(in Python even a single character is treated as a string)
print("b is greater than a")

if 100.5 > 100.4999 :	#comparing a floating value with another floating value
print("100.5 is greater than 100.4999")

if 100 > 100.001 :	#comparing an int with a floating value
print("100 is greater than 100.001")
``````

## Output is

``````b is greater than a
100.5 is greater than 100.4999``````

## <= (less than equals to operator)

This operator is also known as less than equal to operator. It compares the values of two operands around it and it returns a boolean value, true, if either of these conditions is true -
• If the operand of the left of <= operator is smaller than operand on the right of <=, or,
• If the operand on the left of <= is equal to the operand on its right.
• if neither of the above conditions is met then this operator returns false.

## A coding example with <= operator

``````# Python <= operator example

if 'a' <= 'b' :		#comparing a string with another string(in Python even a single character is termed as a string)
print("a is less than or equal to b")

if 'z' <= 'z' :		#comparing a string with another string(in Python even a single character is termed as a string)
print("z is less than equal to z")

if 30.5 <= 31 :		#comparing an floating value value with integer
print("30.5 is less than or equal to 31")

if 20.5 <= 20.6 :	#comparing an floating value value with integer
print("20.5 is less than or equal to 20.6")

if 10.5 <= 10.5 :	#comparong a floating value with a floating value
print("10.5 is less than or equal to 10.5")

if 5 <= 5 :		#comparing an integer to an integer
print("5 is less than or equal to 5")

if 6 <= 6.0 :		#comparing an integer with a floating value
print("6 is less than or equal to 5")

if 10 <= 6 :		#comparing an integer with a floating value
print("10 is less than or equal to 6")
``````

## Output is

``````a is less than or equal to b
z is less than equal to z
30.5 is less than or equal to 31
20.5 is less than or equal to 20.6
10.5 is greater or equal to 10.5
5 is less than or equal to 5
6 is less than or equal to 5``````
Note: There should never be any space between < and = of <= operator, or a compile error is thrown.

## >= (greater than equals to operator)

This operator is also known an greater than equal to operator. It compares the values of two operands around it and it returns a boolean value, true, if either of these conditions is true -
• If the operand of the left of >= operator is greater than operand on the right of >=, or,
• If the operand on the left of >= is equal to the operand on its right.
• if neither of the above conditions is met then this operator returns false.

## A coding example with >= operator

``````# Python >= operator example

if 'z' >= 'b' :  	#comparing a string with another string(in Python even a single character is termed as a string)
print("z is greater than or equal to b")

if 'z' >= 'z' :		#comparing a string with another string(in Python even a single character is termed as a string)
print("z is greater than equal to z")

if 50.5 >=50 :		#comparing an floating value with integer
print("50.5 is greater than or equal to 50")

if 19.7 >=21.7 :	#comparing a floating value with a floating value
print("19.7 is greater than or equal to 21.7")

if 10.5 >= 10.5 :	#comparing a floating value with a floating value
print("10.5 is greater or equal to 10.5")

if 5 >= 5.0 :		#comparing an integer to integer
print("5 is greater than or equal to 5.0")

if 6 >= 10 :		#comparing a integer with a floating value
print("6 is greater than or equal to 10")
``````

## Output is

``````z is greater than or equal to b
z is greater than equal to z
50.5 is less than or equal to 50
10.5 is greater or equal to 10.5
5 is greater than or equal to 5.0``````
Note: There should never be any space between > and = of >= operator, or a compile error is thrown.

## == (equals to operator)

This operator is called equals to operator. It compares the values of two operands by checking for their equality. It returns a boolean value true when operands have equal values otherwise it returns false.

## A coding example with == operator

``````# Python == operator example

if 'z' == 'z' : 	#comparing a character to another character
print("z is equal to z")

if 50.5 == 50.5 :   	#comparing an floating value to another floating value
print("50.5 is equal to 50.5")

if 100 == 100.001 :	#comparing an integer with a floating value
print("100 is equal to 100.001")

if 99.9999 == 99.999 :	#comparing a floating value with a floating value
print("99.9999 is  equal to 99.999")

if 5 == 5.0 :		#comparing an integer to floating value
print("5 is equal to 5.0")

if 10 == 10 :		#comparing an integer to integer
print("10 is equal to 10")

if 97 == "ab" :		# A string in python can be compared to another value only using == operator.
print("97 is equal to a")
``````

## Output -

``````z is equal to z
50.5 is equal to 50.5
5 is equal to 5.0
10 is equal to 10``````
Note: There should never be any space between = and = of == operator, or a compile error is thrown.

## != (not equals to operator)

This operator is also known as not equal to operator. It compares the values of its two operands by checking for their inequality. It returns a boolean value true when operands have unequal values otherwise it returns false.

## A coding example with != operator

``````# Python != operator example

if 'z' != 'a' : 		#comparing a character to another character
print("z is not equal to a")

if 100 != 100.001 :		#comparing an integer with a floating value
print("100 is not equal to 100.001")

if 99.9999 != 99.999 :		#comparing a floating value with a floating value
print("99.9999 is  not equal to 99.999")

if 5.00 != 5.0 :		#comparing an integer to floating value
print("5.00 is not equal to 5.0")

if 11 !=10 :			#comparing an integer to an integer
print("11 is not equal to 10")

if 98 != 'hello' :		# A string in python can be compared to another value only using == operator.
print("98 is not equal to a")
``````

## Output -

``````z is not equal to a
100 is not equal to 100.001
99.9999 is  not equal to 99.999
11 is not equal to 10
98 is not equal to a``````
Note : There should never be any space between ! and = of != operator, or a compile error is thrown.

< Prev
Next >
< Python compound assignment operators
Python logical operators >