Relational and Logical Operators

Relational operators:

< Less Than

> Greater Than

== Equal To

<= Less than or equal to

>= Greater Than or equal to

!= not equal to

These operators return a boolean value true or false. They report on the relationship of the two values that surround them.

For instance:

5 < 8 returns True

99 < 12 returns False

These statements can be used in the control expression of an if statement:

if 5 < 6:

print("5 is less than 6")

That's not a great example because the answer is obvious. It's much more useful when you have a value you don't know such as user input:

age=input("Enter your age:")


if age<21:

print("No drinking for you!")

or something like that.

You can combine relational operators with boolean or logic operators:

and, or, not

These operators take boolean values and report on their relationship.

For instance,

5 < 2 or 5 > 1

returns True because one of the two statements is true.

not ( 2 < 5 and 7 < 10 )

returns False because the not negates the result of the and...

Recent Posts

© 2019