Relational Operators

 Relational Operators are used to compare values.

Gives

True
or
False
as the result of a comparison.

These are different relational operators

OperatorName
>Is greater than
<Is less than
==Is equal to
<=Is less than or equal to
>=Is greater than or equal to
!=Is not equal to

Code

print(5 < 10)
print(2 > 1)
PYTHON

Output

True
True

Possible Mistakes

Mistake - 1

Code

print(3 = 3)
PYTHON

Output

SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

Mistake - 2

Code

print(2 < = 3)
PYTHON

Output

SyntaxError: invalid syntax

Space between relational operators

==
,
>=
,
<=
,
!=
is not valid in Python.

Comparing Numbers

Code

print(2 <= 3)
print(2.53 >= 2.55)
PYTHON

Output

True
False

Comparing Integers and Floats

Code

print(12 == 12.0)
print(12 == 12.1)
PYTHON

Output

True
False

Comparing Strings

Code

print("ABC" == "ABC")
print("CBA" != "ABC")
PYTHON

Output

True
True

Case Sensitive

Code

print("ABC" == "abc")
PYTHON

Output

False

Python is case sensitive. It means

X
(Capital letter) and
x
(small letter) are not the same in Python.

Strings and Equality Operator

Code

print(True == "True")
print(123 == "123")
print(1.1 == "1.1")
PYTHON

Output

False
False
False

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form