Learn Core Python

Relational Operators

Relational Operators:-

à Relational operators are : >,<,>=,<=

à These operators compares values and returns boolean value True or False.

E.g:-

a = 10
b = 5
print(a>b) # True
print(a<b) # False
print(a>=b) #True
print(a<=b) #False

 

à We can use relational operators for str types also.

E.g:-

a = 'cfamily'
b = "cfamily"
print(a>b)
print(a<b)
print(a>=b)
print(a<=b)
print('a'>'A') #imp: lowercase 'a' ascii value is greter than upper case ascii value

 

à Comparing one type with another type

print(True<True)
print(True>False)
print(10>True)
print(0<=False)
print(65>='A')

O/P:-

False

True

True

True

TypeError: '>=' not supported between instances of 'int' and 'str'

We cannot compare numeric value with string object.

 

à Chaining of relational operators is possible.

    In Chaining all comparisons returns True then only output is True otherwise output

    False.

E.g:-

print(10<20)
print(10<20<30)
print(10<20<30<40)
print(10<20<30<30<40>50)

 

Note:- mainly we use relational operators in the conditions.

E.g:-

a = 10
b = 20
if a>b:
    print("a is big")
else:
    print("b is big")

O/P:-

b is big