Chapter 3: Boolean Logic#
Boolean logic is fundamental to programming and decision-making in Python. Every Boolean expression evaluates to either True or False - there are no other possibilities.
Real-world example: If you’re monitoring a reactor temperature and need to trigger a safety shutdown when temperature > 100°C, you’ll use Boolean logic:
temperature = 105.2
shutdown_needed = temperature > 100 # True or False
temperature = 105.2
shutdown_needed = temperature > 100
print(shutdown_needed)
if shutdown_needed == "True":
print("ALARM: let's shut down the reactor")
True
True, False
type(True)
bool
type('True')
str
type(True)
bool
True == 1
False == 0
True
3.1 Boolean Data Type#
In Python, Boolean is a data type that can have only two values:
TrueFalse
Important: Notice the capitalization! Python is case-sensitive:
✅
TrueandFalse(correct)❌
true,false,TRUE,FALSE(incorrect)
print("=== Boolean Values ===")
# Creating Boolean variables
is_reactor_running = True
safety_check_passed = False
print(f"Reactor running: {is_reactor_running}")
print(f"Safety check: {safety_check_passed}")
=== Boolean Values ===
Reactor running: True
Safety check: False
print(f"\n=== Type Checking ===")
print(f"Type of True: {type(True)}")
print(f"Type of False: {type(False)}")
print(f"Type of is_reactor_running: {type(is_reactor_running)}")
=== Type Checking ===
Type of True: <class 'bool'>
Type of False: <class 'bool'>
Type of is_reactor_running: <class 'bool'>
print(f"\n=== Boolean as Numbers ===")
# In Python, True = 1, False = 0
print(f"True as number: {int(True)}")
print(f"False as number: {int(False)}")
print(f"True + True: {True + True}")
print(f"True + False: {True + False}")
=== Boolean as Numbers ===
True as number: 1
False as number: 0
True + True: 2
True + False: 1
print(f"\n=== Common Mistake: Case Sensitivity ===")
# Python is case-sensitive - True/False must be capitalized
# Wrong way (will cause NameError)
try:
result = true # NameError! 'true' is not defined
except NameError as e:
print(f"❌ Error: {e}")
print(" 'true' is not recognized - Python doesn't know what it means")
# Correct way
result = True # ✓ Correct - capitalized
print(f"\n✓ Correct: result = True")
print(f" Value: {result}, Type: {type(result)}")
# Another common mistake
try:
flag = FALSE # NameError! Must be 'False', not 'FALSE'
except NameError as e:
print(f"\n❌ Error: {e}")
print(" Use 'False', not 'FALSE'")
print(f"\n📝 Remember: Always capitalize - True and False")
=== Common Mistake: Case Sensitivity ===
❌ Error: name 'true' is not defined
'true' is not recognized - Python doesn't know what it means
✓ Correct: result = True
Value: True, Type: <class 'bool'>
❌ Error: name 'FALSE' is not defined
Use 'False', not 'FALSE'
📝 Remember: Always capitalize - True and False
3.2 Comparison Operators#
Comparison operators compare two values and return a Boolean result (True or False). These are essential for making decisions in your code.
3.2.1 Basic Comparison Operators#
Operator |
Description |
Example |
Result |
|---|---|---|---|
|
Equal to |
|
|
|
Not equal to |
|
|
|
Less than |
|
|
|
Greater than |
|
|
|
Less than or equal |
|
|
|
Greater than or equal |
|
|
Important:
Use
==for comparison, not=(which is for assignment)These work with numbers, strings, and other data types
1 == 2 #'hello world'
1 == 1
True
print(5 != 3)
print(5 == 3)
print(5 > 3)
print(3 < 5)
print(5 >= 5)
True
False
True
True
True
1.0, 1
(1.0, 1)
True == 1
1.0 == 1
print(False == 1.0)
False
'hello' == 'Hello'
False
'abc' < 'cde'
True
print("=== Basic == Operator Examples ===")
print(f"1 == 2: {1 == 2}") # False
print(f"5 == 5: {5 == 5}") # True
print(f"3.14 == 3.14: {3.14 == 3.14}") # True
=== Basic == Operator Examples ===
1 == 2: False
5 == 5: True
3.14 == 3.14: True
print(f"\n=== String Comparisons ===")
print(f"'Hello' == 'Hello': {'Hello' == 'Hello'}") # True
print(f"'Hello' == 'hello': {'Hello' == 'hello'}") # False (case sensitive)
print(f"'Python' == 'Python': {'Python' == 'Python'}") # True
=== String Comparisons ===
'Hello' == 'Hello': True
'Hello' == 'hello': False
'Python' == 'Python': True
print("=== Number Comparisons ===")
temperature = 85.5 # C
pressure = 2.3 #bar
flow_rate = 125.0 # m3/min
print(f"Temperature: {temperature}°C")
print(f"Temperature == 85.5: {temperature == 85.5}")
print(f"Temperature != 90.0: {temperature != 90.0}")
print(f"Temperature > 80: {temperature > 80}")
print(f"Temperature < 100: {temperature < 100}")
print(f"Temperature >= 85.5: {temperature >= 85.5}")
print(f"Temperature <= 85.0: {temperature <= 85.0}")
=== Number Comparisons ===
Temperature: 85.5°C
Temperature == 85.5: True
Temperature != 90.0: True
Temperature > 80: True
Temperature < 100: True
Temperature >= 85.5: True
Temperature <= 85.0: False
print(f"\n=== String Comparisons ===")
chemical1 = "Benzene"
chemical2 = "benzene"
chemical3 = "Benzene"
print(f"'{chemical1}' == '{chemical3}': {chemical1 == chemical3}")
print(f"'{chemical1}' == '{chemical2}': {chemical1 == chemical2}") # Case sensitive!
print(f"'{chemical1}' != '{chemical2}': {chemical1 != chemical2}")
=== String Comparisons ===
'Benzene' == 'Benzene': True
'Benzene' == 'benzene': False
'Benzene' != 'benzene': True
print(f"\n=== Alphabetical Comparison (Strings) ===")
print(f"'Apple' < 'Banana': {'Apple' < 'Banana'}") # Alphabetical order
print(f"'apple' < 'Apple': {'apple' < 'Apple'}") # Lowercase vs uppercase
=== Alphabetical Comparison (Strings) ===
'Apple' < 'Banana': True
'apple' < 'Apple': False
3.3 Logical Operators#
Logical operators combine multiple Boolean expressions to create more complex conditions. There are three main logical operators in Python:
3.3.1 The and Operator#
Returns True only when both conditions are True.
Truth Table for and:
A |
B |
A and B |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Example: A reactor is safe to operate when temperature < 100°C AND pressure < 5 bar.
A = True
B = False
A and B
False
# reactor
temp = 100 # C
press = 20 # bar
temp_threshold = 90
press_threshold = 10
print(temp > temp_threshold)
print(press > press_threshold)
if temp > temp_threshold and press > press_threshold:
print('ALARM: Shut down the reactor')
else:
print('Nothing')
True
True
ALARM: Shut down the reactor
print("=== AND Operator Examples ===")
temperature = 85.0
pressure = 3.2
print(f"Temperature: {temperature}°C, Pressure: {pressure} bar")
print(f"Temperature < 100: {temperature < 100}")
print(f"Pressure < 5: {pressure < 5}")
print(f"Safe operation (both conditions): {temperature < 100 and pressure < 5}")
=== AND Operator Examples ===
Temperature: 85.0°C, Pressure: 3.2 bar
Temperature < 100: True
Pressure < 5: True
Safe operation (both conditions): True
3.3.2 The or Operator#
Returns True when at least one condition is True.
Truth Table for or:
A |
B |
A or B |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Example: An alarm should sound when temperature > 90°C OR pressure > 10 bar.
# reactor
temp = 100 # C
press = 2 # bar
temp_threshold = 90
press_threshold = 10
print(temp > temp_threshold)
print(press > press_threshold)
if temp > temp_threshold or press > press_threshold:
print('ALARM: Shut down the reactor')
else:
print('Nothing')
True
False
ALARM: Shut down the reactor
# Different scenario
temperature2 = 105.0
pressure2 = 2.0
print(f"\nTemperature: {temperature2}°C, Pressure: {pressure2} bar")
print(f"Temperature < 100: {temperature2 < 100}")
print(f"Pressure < 5: {pressure2 < 5}")
print(f"Safe operation (both conditions): {temperature2 < 100 and pressure2 < 5}")
print(f"\n=== OR Operator Examples ===")
print(f"Temperature: {temperature2}°C, Pressure: {pressure2} bar")
print(f"Temperature > 100: {temperature2 > 100}")
print(f"Pressure > 5: {pressure2 > 5}")
print(f"Alarm needed (either condition): {temperature2 > 100 or pressure2 > 5}")
Temperature: 105.0°C, Pressure: 2.0 bar
Temperature < 100: False
Pressure < 5: True
Safe operation (both conditions): False
=== OR Operator Examples ===
Temperature: 105.0°C, Pressure: 2.0 bar
Temperature > 100: True
Pressure > 5: False
Alarm needed (either condition): True
3.3.3 The not Operator#
Reverses the Boolean value: True becomes False, False becomes True.
Truth Table for not:
A |
not A |
|---|---|
|
|
|
|
Example: Equipment is offline when it’s NOT running.
temp = 100 # C
press = 2 # bar
temp_threshold = 90
press_threshold = 10
# print(temp > temp_threshold)
# print(press > press_threshold)
print(temp < temp_threshold)
print(not temp < temp_threshold)
False
True
# No alarm case
temperature3 = 80.0
pressure3 = 2.0
print(f"\nTemperature: {temperature3}°C, Pressure: {pressure3} bar")
print(f"Temperature > 100: {temperature3 > 100}")
print(f"Pressure > 5: {pressure3 > 5}")
print(f"Alarm needed (either condition): {temperature3 > 100 or pressure3 > 5}")
print(f"\n=== NOT Operator Examples ===")
is_running = True
is_maintenance_mode = False
print(f"Equipment running: {is_running}")
print(f"NOT running: {not is_running}")
print(f"Maintenance mode: {is_maintenance_mode}")
print(f"NOT in maintenance: {not is_maintenance_mode}")
Temperature: 80.0°C, Pressure: 2.0 bar
Temperature > 100: False
Pressure > 5: False
Alarm needed (either condition): False
=== NOT Operator Examples ===
Equipment running: True
NOT running: False
Maintenance mode: False
NOT in maintenance: True
3.4 Truthiness and Boolean Conversion#
In Python, every value has a “truthiness” - it can be evaluated as True or False in a Boolean context. This is very useful for checking if variables have values or are empty.
bool(1)
True
bool(0)
False
bool('hello')
True
bool('e')
True
bool('')
False
3.4.1 “Falsy” Values (evaluate to False)#
These values are considered False in Boolean contexts:
False(obviously)None(Python’s “nothing” value)0(zero for any numeric type)""(empty string)[](empty list){}(empty dictionary)set()(empty set)
print("=== Testing Falsy Values ===")
falsy_values = [False, None, 0, 0.0, "", [], {}]
print(bool(False))
print(bool(None))
print(bool(0))
print(bool(0.0))
print(bool([]))
=== Testing Falsy Values ===
False
False
False
False
False
print("=== Testing Falsy Values ===")
falsy_values = [False, None, 0, 0.0, "", [], {}]
for value in falsy_values:
print(f"bool({repr(value)}) = {bool(value)}")
=== Testing Falsy Values ===
bool(False) = False
bool(None) = False
bool(0) = False
bool(0.0) = False
bool('') = False
bool([]) = False
bool({}) = False
3.4.2 “Truthy” Values (evaluate to True)#
Everything else is considered True:
True(obviously)Any non-zero number
Any non-empty string
Any non-empty collection (list, dict, set, etc.)
print(bool('hello'))
print(bool('e'))
print(bool(''))
True
True
False
print(f"\n=== Testing Truthy Values ===")
truthy_values = [True, 1, -1, 3.14, "hello", " ", [1, 2], {"key": "value"}]
for value in truthy_values:
print(f"bool({repr(value)}) = {bool(value)}")
=== Testing Truthy Values ===
bool(True) = True
bool(1) = True
bool(-1) = True
bool(3.14) = True
bool('hello') = True
bool(' ') = True
bool([1, 2]) = True
bool({'key': 'value'}) = True
def dummy_function():
return 10
x = dummy_function() # 10
print(x == True)
if x == True:
print('do something - 1')
if x:
print('do something - 2')
False
do something - 2
Why is this useful?
Check if a string has content:
if name:instead ofif name != ""Check if a list has items:
if data_list:instead ofif len(data_list) > 0Check if a variable was assigned:
if value:instead ofif value is not None
3.5 any() and all()#
any() returns True when:
At least one element is truthy
Example:
any([False, True, False])→True
all() returns True when:
All elements are truthy
Example:
all([True, True, True])→True
Remember:
any([])→False(no elements to be true)all([])→True(no elements to be false)Both work with any iterable (lists, tuples, generators, etc.)
Very useful with list comprehensions and generator expressions
any([False, False, True])
True
temperature = 90
pressure = 10
flow_rate = 50
if any([temperature > 100, pressure > 20, flow_rate > 30]):
print('shut down the reactor')
# if temperature > 100 or pressure > 20 or flow_rate > 30:
# print('shut down the reactor')
shut down the reactor
temperature = 90
pressure = 10
flow_rate = 50
if all([temperature > 100, pressure > 20, flow_rate > 30]):
print('shut down the reactor')
all([True, True, False])
False
any([0, 0, 0.0])
#any([bool(0) , bool(0), bool(0.0)]) # any([True, True, True])
False
any([True, True, False])
True
all([True, True, False])
False
all([True, True, True])
True
With numeric inputs, any() and all() treat the values as Booleans, as explained in …
any([0, 1, 0])
True
3.6 Text inclusion#
Python provides the in operator to check for inclusion. For example, if we want to determine whether a molecular formula contains nickel, we can simply test whether "Ni" is present in the formula.
in
Cell In[47], line 1
in
^
SyntaxError: invalid syntax
'hello' in 'hello world'
True
'h' in 'yellow'
False
formulas
# Check for presence of nickel
print("Nickel is present in the formula:", "Ni" in formula)
print("Palladium is present in the formula:", "Pd" in formula)
print("Nickel is not present in the formula? ", "Ni" not in formula)
print("Palladium is not present in the formula:", "Pd" not in formula)
Nickel is present in the formula: True
Palladium is present in the formula: False
Nickel is not present in the formula? False
Palladium is not present in the formula: True