In fact, it’s actually pretty cool once you get the hang of it!
So what is Boolean algebra anyway? Well, in simple terms, it’s a way to manipulate true and false statements using logical operators like AND, OR, NOT, XOR (exclusive or), and NAND (not-and). And guess what Python has all these operators built right into the language!
Let’s start with some basic examples. First up is the AND operator, which returns True only if both operands are true:
# This script demonstrates the use of logical operators in Python.
# First, we define two variables and use the AND operator to check if both conditions are true.
x = 5 > 3 and 7 < 10 # The AND operator returns True only if both operands are true.
print(x) # Output: True
In this case, we’re checking whether `x` (which is assigned the result of a boolean expression) is greater than or equal to 3 AND less than or equal to 10. Since both conditions are true, the output is True.
Next up is the OR operator, which returns True if either operand is true:
# Checking if x is greater than or equal to 3 AND less than or equal to 10
x = 3 <= 5 <= 10 # Assigning the result of the boolean expression to x
print(x) # Output: True
# Using the OR operator to check if either operand is true
x = 5 > 3 or 7 < 10 # Assigning the result of the boolean expression to x
print(x) # Output: True
In this case, we’re checking whether `x` (which is assigned the result of a boolean expression) is greater than or equal to 3 OR less than or equal to 10. Since either condition is true (5 > 3), the output is True.
Now NOT also known as negation or complement. This operator returns the opposite of its operand:
# Checking if x is greater than or equal to 3 OR less than or equal to 10
x = 5 # Assigning x a value of 5
if x >= 3 or x <= 10: # Checking if either condition is true
print(True) # Output: True
# Using the NOT operator to return the opposite of its operand
x = not(True) # Assigning x the opposite of True, which is False
print(x) # Output: False
In this case, we’re checking whether `x` (which is assigned the result of a boolean expression) is NOT equal to True. Since it isn’t, the output is False.
Finally, XOR and NAND which are less commonly used in Python but still worth mentioning for completeness sake!
XOR (exclusive or) returns True if exactly one operand is true:
# Assigning the result of a boolean expression to variable x
x = (5 > 3) ^ (7 < 10) # XOR operator used to compare two boolean expressions
# Printing the value of x
print(x) # Output: False
# XOR (exclusive or) returns True if exactly one operand is true
# In this case, both operands are false, hence the output is False.
In this case, we’re checking whether `x` (which is assigned the result of a boolean expression) is greater than or equal to 3 XOR less than or equal to 10. Since both conditions are true (5 > 3 AND 7 < 10), the output is False because there are more than one True values! NAND (not-and) returns True if neither operand is true:
# This script checks whether x is greater than or equal to 3 XOR less than or equal to 10.
# XOR (exclusive or) returns True if only one of the operands is true.
# Assign the result of the boolean expression to x.
x = (5 > 3) ^ (7 < 10) # x is now True because only one of the conditions is true.
# Use the NAND (not-and) operator to check if neither operand is true.
# Since both conditions are true, the output will be False.
x = not(5 > 3 and 7 < 10)
# Print the value of x.
print(x) # Output: False
In this case, we’re checking whether `x` (which is assigned the result of a boolean expression) is NOT equal to True AND False. Since it isn’t (because either condition could be true), the output is True because there are no True values!
And that’s all for today, I hope you enjoyed this brief introduction to Python’s Boolean algebra. Remember: keep your code simple and readable, and always use comments to explain what each line does.