Lab 02: Booleans and Strings#
Topics Covered:
Boolean values and logical operations
Comparison operators for process conditions
String manipulation for chemical data
Formatting output for engineering reports
1. Demonstration#
1.1 Boolean Values and Logic#
Boolean values (True and False) are essential for decision-making in process control and safety systems. In chemical engineering, we constantly check conditions:
Is the temperature above the flash point?
Is the pressure within safe operating limits?
Has the reaction reached equilibrium?
Example 1.1.1: Logical Operators#
Combine boolean values using logical operators:
Operator |
Meaning |
Example |
|---|---|---|
|
Both must be True |
|
|
At least one True |
|
|
Inverts the value |
|
# Safety interlock system for a distillation column
temperature_ok = True # Temperature within range
pressure_ok = True # Pressure within range
level_ok = False # Liquid level NOT within range (alarm!)
Example 1.1.2: Booleans and Numbers#
In Python, True equals 1 and False equals 0. This is useful for counting conditions.
# Count how many sensors are in alarm state
sensor1_alarm = True
sensor2_alarm = False
sensor3_alarm = True
sensor4_alarm = False
1.2 Strings (Text Data)#
Strings are sequences of characters used for:
Chemical formulas and compound names
Equipment labels and tag numbers
Process descriptions and log messages
Data file handling and report generation
Example 1.2.1: String Methods#
Strings have built-in methods for manipulation:
Method |
Description |
Example |
|---|---|---|
|
Convert to uppercase |
|
|
Convert to lowercase |
|
|
Remove whitespace |
|
|
Replace substring |
|
|
Split into list |
|
# Working with chemical data
raw_formula = " ch4 " # Messy input data
Example 1.2.2: Searching in Strings#
Check if a substring exists or find its position.
# Process log message
log_message = "ALARM: Reactor temperature exceeded 250°C at 14:32:05"
1.3 Format a report message#
Example 1.3.1: Basic f-string Usage#
Prefix string with f and use {variable} to insert values.
# Reactor operating conditions
reactor_id = "R-101"
temperature = 185.7
pressure = 3.45
conversion = 0.923
Example 1.3.2: Number Formatting in f-strings#
Control decimal places and formatting using format specifiers:
Format |
Description |
Example |
Output |
|---|---|---|---|
|
2 decimal places |
|
|
|
4 decimal places |
|
|
|
Scientific notation |
|
|
|
Percentage |
|
|
|
Thousands separator |
|
|
# Heat exchanger calculations
heat_duty = 1256789.5 # Watts
efficiency = 0.8734
flow_rate = 0.0000523 # m³/s
1.4 Chemical Engineering Applications#
Example 1.4.1: Safety Interlock Logic#
Design a safety system that checks multiple conditions before allowing operation.
# Reactor safety interlock system
# Operating limits
T_min, T_max = 150.0, 250.0 # °C
P_min, P_max = 1.0, 10.0 # bar
level_min, level_max = 20.0, 80.0 # %
# Current readings
T_current = 185.0 # °C
P_current = 5.5 # bar
level_current = 45.0 # %
Example 1.4.2: Material Classification#
Use string operations to parse and classify chemical information.
# Chemical compound database entry
compound_data = "Ethanol;C2H5OH;78.37;46.07;Flammable"
Example 1.4.3: Product Specification Check#
Verify if a product meets quality specifications.
# Product quality specifications
product_name = "Industrial Grade Acetone"
batch_number = "ACE-2024-0156"
2. Practice Problems#
Now it’s your turn! Solve the following problems in the cells below. Make sure to:
Use appropriate variable names
Include comments explaining your logic
Format output clearly with units
Problem 1: Flow Regime Classification#
Based on the Reynolds number, classify the flow regime in a pipe:
Given:
Reynolds number (Re) = 3500
Classification criteria:
Re < 2300 → Laminar flow
2300 ≤ Re ≤ 4000 → Transitional flow
Re > 4000 → Turbulent flow
Tasks:
Create boolean variables:
is_laminar,is_transitional,is_turbulentPrint the Reynolds number and which booleans are True
Create and print a status string that says “Flow regime: [regime type]”
# Your solution here
# Given
Re = 3500
Problem 2: Chemical Formula Parser#
Parse information from a chemical formula string.
Given:
formula = "Ca(OH)2"
Tasks:
Find the length of the formula string
Check if the formula contains:
Calcium (“Ca”) - store as
has_calciumOxygen (“O”) - store as
has_oxygenNitrogen (“N”) - store as
has_nitrogen
Extract the first 2 characters (the metal symbol)
Check if the formula contains parentheses (indicating a polyatomic ion)
Print all results with clear labels
# Your solution here
formula = "Ca(OH)2"
Problem 3: Process Log Cleaner#
Clean and standardize messy process log entries from a plant database.
Given:
log_entry = " WARNING: pump-101 flow rate LOW at 14:25 "
Tasks:
Remove leading and trailing whitespace using
.strip()Convert the entire message to uppercase using
.upper()Replace “PUMP-101” with “PMP-101” (standardized equipment code) using
.replace()Replace “LOW” with “BELOW SETPOINT” for clarity
Check if the cleaned message starts with “WARNING” using
.startswith()Check if the message contains a time (look for “:” character)
Split the message by spaces and count how many words it contains
Print each transformation step and the final results
# Your solution here
# Given
log_entry = " WARNING: pump-101 flow rate LOW at 14:25 "
Problem 4: Reaction Yield Analysis#
Analyze a batch reaction and determine if it meets production targets.
Given data:
Reaction: “Esterification of Acetic Acid”
Reactor ID: “R-201”
Theoretical yield: 500.0 kg
Actual yield: 423.5 kg
Target yield: 80%
Tasks:
Calculate the percent yield: (actual/theoretical) × 100
Determine if yield is acceptable (≥ 80%): store as boolean
yield_acceptableCreate a result string that combines the reactor ID and reaction name
Print the theoretical yield, actual yield, and percent yield (use 1 decimal place)
Print whether the yield is acceptable and display “ACCEPTABLE” or “BELOW TARGET”
# Your solution here
# Given data
reaction = "Esterification of Acetic Acid"
reactor_id = "R-201"
theoretical_yield = 500.0 # kg
actual_yield = 423.5 # kg
Problem 5: Equipment Tag Validator#
Validate equipment tag numbers follow the correct format.
Standard format: XXX-NNN-YY where:
XXX= 3-letter equipment type code (must be uppercase)NNN= 3-digit unit numberYY= 2-character identifier
Given tags to validate:
tag1 = "PMP-101-AB"
tag2 = "pump-101-AB"
tag3 = "HX-42-C"
Tasks for each tag:
Check if total length is exactly 10 characters
Check if the first 3 characters are uppercase letters (use
.isupper())Check if position 4 and 8 are hyphens (index 3 and 7)
Store overall validity as a boolean (all conditions must be True)
Print validation results for each tag
# Your solution here
# Given tags to validate
tag1 = "PMP-101-AB"
tag2 = "pump-101-AB"
tag3 = "HX-42-C"