Relational Operators in Python

Introduction

Relational operators in Python are used to create a relationship between two entities, usually involving comparisons such as equality, inequality, greater than, and less than. The operators then evaluate the relationship between the entities and give an output of either True or False for the relationship.

Relational Operators in Python

  1. Equal To: ==
    This operator checks if both operands are equal. The syntax for the equal to operator is a == b.

  2. Not Equal To: !=
    This operator checks if both operands are not equal. The syntax for the not equal to operator is a != b.

  3. Greater Than: >
    This operator checks if the operand on the left is greater than the operand on the right. The syntax for the greater than operator is a > b.

  4. Less Than: <
    This operator checks if the operand on the left is less than the operand on the right. The syntax for the less than operator is a < b.

  5. Greater Than or Equal To: >=
    This operator checks if the operand on the left is greater than or equal to the operand on the right. The syntax for the greater than or equal to operator is a >= b.

  6. Less Than or Equal To: <=
    This operator checks if the operand on the left is less than or equal to the operand on the right. The syntax for the less than or equal to operator is a <= b.

Examples Using Relational Operators in Python

a = 5
b = 10

#  1. Equal to: ==
if a == b:
    print("True, a equals b")
else: 
    print("False, a does not equal b")
 

#2. Not equal to: !=
if a != b:
    print("True, a does not equal b")
else:
    print("False, a equals b")

# 3. Greater than: >
if a > b:
    print("True, a is greater than b")
else:    
    print("False, a is not greater than b")

# 4. Less than: <
if a < b:
    print("True, a is less than b")
else:   
    print("False, a is not less than b")

# 5. Greater than or equal to: >=
if a >= b:
    print("True, a is greater than or equal to b")
else:   
    print("False, a is not greater than or equal to b")

# 6. Less than or equal to: <=
if a <= b:
    print("True, a is less than or equal to b")
else:   
    print("False, a is not less than or equal to b")

Logical Operators

NOT FUNCTION


What is it?

The NOT function for booleans is a logical operator that inverts the value of a boolean expression. In simple terms:

If the input is TRUE, the NOT function returns FALSE. If the input is FALSE, the NOT function returns TRUE. The NOT function operates on a single boolean value (it is a unary operator).

In Boolean algebra:

NOT(A)=Β¬A

Where

  • A is a boolean value, and
  • ¬𝐴 is the negation of 𝐴

For example:

NOT(TRUE) = FALSE

NOT(FALSE) = TRUE

In programming languages, the NOT function is often represented by symbols like ! or the keyword not. For example:

  • In C, C++, Java, etc.: !TRUE == FALSE
  • In Python: not True == False

### Real World Example:

Essentially the NOT function is an INVERSE function

  • You put your shoes on in the morning, and you take your shoes off in the night
  • Wrapping a present before Christmas, unwrapping a present after Christmas
  • Setting up decorations for Halloween, taking out decorations after Halloween
  • When you convert Celcius to Farenheit, you are multiplying and adding, but when you convert Farenhiet to Celcius, you are subtracting and adding.

### Coding Example in Python:

# Initial state of the light (False means OFF, True means ON)
light_on = False

def toggle_light():
    global light_on
    # Toggle the light state using the NOT function
    light_on = not light_on
    # Output the current state of the light
    if light_on:
        print("The light is now ON.")
    else:
        print("The light is now OFF.")

print("Press Enter to toggle the light switch. Type 'exit' to stop.")

while True:
    user_input = input()  # Simulate button press
    if user_input.lower() == 'exit':
        break
    toggle_light()

AND FUNCTION

_____________________-

What is it?

The Boolean AND function is a logical operation that outputs true if both of its operands are true; otherwise, it outputs false.

Here’s the formal definition of the Boolean AND function for two operands

𝐴 and 𝐡:

𝐴∧𝐡 = true if and only if both 𝐴 = true and 𝐡 = true 𝐴∧𝐡 = false if 𝐴 = false or 𝐡 = false.

If both 𝐴 and 𝐡 are true, then 𝐴∧𝐡 = true If 𝐴 is false or 𝐡 is false, or 𝐴 and 𝐡 is false, then 𝐴∧𝐡 = false

Real World Example:

  • You have to scan your keycard and enter a pin to access a safe
  • When you want to throw away your trash, you need to open the trash can and throw away your trash
  • To use your phone, you must turn it on and put your passcode in

Coding Example in Python

def check_access(keycard_scanned, pin_correct):
    if keycard_scanned and pin_correct:
        return "Access Granted"
    else:
        return "Access Denied"

keycard_scanned = True  
pin_correct = False     

result = check_access(keycard_scanned, pin_correct)
print(result)  

keycard_scanned = True  
pin_correct = True    

result = check_access(keycard_scanned, pin_correct)
print(result)  

OR


What is it?

The Boolean OR function is a logical operation that evaluates two or more Boolean values (true or false) and returns:

True if at least one of the input values is true. False if all input values are false.

The Boolean OR function is inclusive since it returns true if one of or both inputs are true. So as long as one condition is met, the outcome is positive (True).

Real World Example:

Conditions:

  • It is sunny outside
  • It is hot outside

If one of these conditions are met, I will go outside to swim.

If both of these conditions are not met, I will not go outside to swim.

Coding Example in Python:

# Conditions #1
is_sunny = True  
is_warm = False   

#Example #1
if is_sunny or is_warm:
    result = "Swim outside"
else:
    result = "Do not swim outside"

print(result)

# Conditions #2
is_sunny = False  
is_warm = False   

#Example #2
if is_sunny or is_warm:
    result = "Swim outside"
else:
    result = "Do not swim outside"

print(result)

Conclusion

In this lesson, we learned about relational and logical operators in Python. Relational operators such as ==, !=, >, <, >=, and <= are used to compare values and determine relationships between them. Logical operators like β€œand”, β€œor” ,and β€œnot” are used to combine multiple differnt boolean expressions together and control the program’s flow based on the conditons. Understanding how to use relational and logical operators are key for making decisions in code and implementing conditional logic.