Relational and Boolean Operators

Relational Operators

greater_than = 7 > 4               # True (7 is greater than 4)
less_than = 8 < 10                 # True (8 is less than 10)
greater_than_equal_to = 7 >= 7     # True (7 is equal to 7)
less_than_equal_to = 7 <= 7        # True (7 is equal to 7)

print(greater_than)
print(less_than)
print(greater_than_equal_to)
print(less_than_equal_to)

Output:


Boolean AND Operator (and)

and1 = (7 > 5) and (7 < 5)         # True and False β†’ False
and2 = (7 > 5) and (7 > 5)         # True and True β†’ True

print(and1, and2)

Output:


Boolean OR Operator (or)

or1 = (7 > 5) or (7 < 5)           # True or False β†’ True
or2 = (7 > 5) or (7 > 5)           # True or True β†’ True

print(or1, or2)

Output:


Not Operator (not)

not1 = not True                   # β†’ False
not2 = not False                  # β†’ True

print(not1, not2)

Output:


βœ… Summary:

  • Relational operators compare values (>, <, >=, <=).

  • Boolean operators combine expressions:

    • and: True if both sides are True.

    • or: True if at least one side is True.

    • not: Reverses the Boolean value.

#!/bin/python3

#Relational and Boolean Operators
greater_than = 7 > 4
less_than = 8 < 10
greater_than_equal_to = 7 >= 7
less_than_equal_to = 7 <= 7

print(greater_than)
print(less_than)
print(greater_than_equal_to)
print(less_than_equal_to)


#And Operator
and1 = (7 > 5) and (7 < 5)
and2 = (7 > 5) and (7 > 5)
print(and1,and2)


#Or Operator
or1 = (7 > 5) or (7 < 5)
or2 = (7 > 5) or (7 > 5)
print(or1,or2)


#Not Operator
not1 = not True
not2 = not False
print(not1,not2)

True Table Cheatsheet

Last updated