Fall 2024 - P4
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.5 Python Booleans
Student led teaching on Python Booleans.
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
-
Equal To:
==
This operator checks if both operands are equal. The syntax for the equal to operator isa == b
. -
Not Equal To:
!=
This operator checks if both operands are not equal. The syntax for the not equal to operator isa != b
. -
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 isa > b
. -
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 isa < b
. -
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 isa >= b
. -
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 isa <= 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.