Lab 03: Boolean Logic and Logical Operators
In this lab, you will explore Boolean logic and logical operators in Python. You can use any of the following environments to complete the exercises:
- Visual Studio Code (VS Code)
- Google Colab
- Jupyter Notebook
- PyCharm
- Anaconda
Setup Instructions
1. Visual Studio Code (VS Code)
- Installation: Ensure Python is installed and the Python extension is added to VS Code.
- Create a New File: Open VS Code and create a new Python file (e.g.,
lab03.py
). - Run the Code: Use the terminal or the run button in the editor to execute your code.
2. Google Colab
- Access Colab: Go to Google Colab.
- Create a New Notebook: Click on “New Notebook.”
- Run Cells: Write your code in a cell and run it by clicking the play button.
3. Jupyter Notebook
- Installation: Install Jupyter Notebook via Anaconda or pip.
- Start Jupyter: Open your terminal and type
jupyter notebook
to launch. - Create a New Notebook: Create a new Python notebook and write your code in the cells.
4. PyCharm
- Installation: Download and install PyCharm.
- Create a New Project: Start a new project and create a new Python file.
- Run the Code: Write and run your code using the built-in run configuration.
5. Anaconda
- Launch Anaconda Navigator: Open Anaconda Navigator.
- Choose an Environment: Launch either Jupyter Notebook or Spyder.
- Create a New File/Notebook: Write your code in a new notebook or script.
Exercises
Exercise 1: Basic Boolean Operations
Task:
- Create two Boolean variables,
a
andb
. - Print the results of the following operations:
a AND b
a OR b
NOT a
Example Code:
# Define Boolean variables
a = True
b = False
# Print results of Boolean operations
print("a AND b:", a and b)
print("a OR b:", a or b)
print("NOT a:", not a)
Exercise 2: Logical Comparisons
Task:
- Ask the user for two numbers.
- Compare the numbers using logical operators and print the results.
Example Code:
# Get user input for two numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Perform comparisons
print("Is num1 greater than num2?", num1 > num2)
print("Is num1 less than or equal to num2?", num1 <= num2)
print("Are num1 and num2 equal?", num1 == num2)
Exercise 3: Combining Conditions
Task:
- Ask the user for their age and whether they have a driver’s license.
- Determine if they are eligible to drive.
Example Code:
# Get user input for age and license status
age = int(input("Enter your age: "))
has_license = input("Do you have a driver's license? (yes/no): ").lower() == 'yes'
# Determine eligibility to drive
eligible_to_drive = age >= 18 and has_license
print("Eligible to drive:", eligible_to_drive)
Running the Code
- Copy the Code: Copy the code examples provided above into your Python file.
- Run the Program: Execute the program in your IDE. You can usually do this by clicking a “Run” button or using a keyboard shortcut (like
Shift + F10
in PyCharm). - Interact with the Program: Follow the prompts in the console to input your data.
Conclusion
This lab helps you understand Boolean logic and logical operators in Python. By completing these exercises, you will gain practical experience with conditional statements and logical operations. Enjoy coding!
or
Lab 03: Boolean Logic and Logical Operators (Civil Engineering Focus)
Objective: Apply Boolean logic (and
, or
, not
) and relational operators to solve civil engineering problems, such as safety checks, material selection, and design validation.
Key Concepts
- Boolean Operators:
and
: True if both conditions are true.or
: True if at least one condition is true.not
: Inverts the result (True → False, False → True).
- Relational Operators:
>
,<
,==
,!=
,>=
,<=
Task 1: Structural Safety Check
Problem:
A concrete column is considered safe if:
- The applied load ≤ 80% of its ultimate load capacity.
- The material grade is “C30” or higher.
Write a program to determine if the column is safe.
Sample Input:
Enter applied load (kN): 1200
Enter ultimate load capacity (kN): 1500
Enter material grade (e.g., C25, C30): C30
Sample Output:
Safety Check:
- Load Condition: 1200 kN ≤ 1200 kN (80% of 1500 kN) → True
- Material Grade: C30 ≥ C30 → True
Column is SAFE.
Solution Code:
applied_load = float(input("Enter applied load (kN): "))
ultimate_capacity = float(input("Enter ultimate load capacity (kN): "))
material_grade = input("Enter material grade (e.g., C25, C30): ")
# Calculate 80% of ultimate capacity
safe_load = 0.8 * ultimate_capacity
# Boolean checks
load_safe = applied_load <= safe_load
material_safe = int(material_grade[1:]) >= 30 # Extract numeric part (e.g., "C30" → 30)
# Final safety verdict
is_safe = load_safe and material_safe
print(f"\nSafety Check:")
print(f"- Load Condition: {applied_load} kN ≤ {safe_load} kN → {load_safe}")
print(f"- Material Grade: {material_grade} ≥ C30 → {material_safe}")
print(f"Column is {'SAFE' if is_safe else 'UNSAFE'}.")
Task 2: Traffic Light Control Logic
Problem:
A traffic light should switch to red if:
- A pedestrian button is pressed, OR
- The vehicle count exceeds 50 in 30 seconds.
Write a program to determine if the light should turn red.
Sample Input:
Pedestrian button pressed? (yes/no): no
Vehicle count in 30s: 55
Sample Output:
Pedestrian Request: False
High Traffic: True (55 > 50)
Switch to RED: True
Solution Code:
pedestrian = input("Pedestrian button pressed? (yes/no): ").lower() == "yes"
vehicle_count = int(input("Vehicle count in 30s: "))
pedestrian_condition = pedestrian
traffic_condition = vehicle_count > 50
switch_red = pedestrian_condition or traffic_condition
print(f"\nPedestrian Request: {pedestrian_condition}")
print(f"High Traffic: {traffic_condition} ({vehicle_count} > 50)")
print(f"Switch to RED: {switch_red}")
Task 3: Soil Suitability Check
Problem:
Soil is suitable for foundation construction if:
- It is not classified as “Organic Clay” (
not
operator). - The bearing capacity ≥ 150 kPa.
Write a program to validate soil suitability.
Sample Input:
Soil type (e.g., Sandy Clay, Organic Clay): Sandy Clay
Bearing capacity (kPa): 180
Sample Output:
Soil Type: Sandy Clay → Not Organic Clay (True)
Bearing Capacity: 180 kPa ≥ 150 kPa → True
Soil is SUITABLE.
Solution Code:
soil_type = input("Soil type (e.g., Sandy Clay, Organic Clay): ")
bearing_capacity = float(input("Bearing capacity (kPa): "))
is_organic = "Organic Clay" in soil_type
bearing_ok = bearing_capacity >= 150
is_suitable = not is_organic and bearing_ok
print(f"\nSoil Type: {soil_type} → Not Organic Clay ({not is_organic})")
print(f"Bearing Capacity: {bearing_capacity} kPa ≥ 150 kPa → {bearing_ok}")
print(f"Soil is {'SUITABLE' if is_suitable else 'UNSUITABLE'}.")
Task 4: Bridge Design Compliance
Problem:
A bridge design passes if either:
- It meets seismic zone requirements and budget ≤ $1M.
OR - It uses innovative materials and has a safety factor ≥ 2.5.
Write a program to evaluate compliance.
Sample Input:
Meets seismic zone requirements? (yes/no): yes
Project budget ($): 950000
Uses innovative materials? (yes/no): no
Safety factor: 3.0
Sample Output:
Condition 1 (Seismic + Budget): True
Condition 2 (Materials + Safety): False
Design PASSES: True
Solution Code:
seismic_compliant = input("Meets seismic zone requirements? (yes/no): ").lower() == "yes"
budget = float(input("Project budget ($): "))
innovative_materials = input("Uses innovative materials? (yes/no): ").lower() == "yes"
safety_factor = float(input("Safety factor: "))
condition1 = seismic_compliant and (budget <= 1e6)
condition2 = innovative_materials and (safety_factor >= 2.5)
design_passes = condition1 or condition2
print(f"\nCondition 1 (Seismic + Budget): {condition1}")
print(f"Condition 2 (Materials + Safety): {condition2}")
print(f"Design PASSES: {design_passes}")
Key Takeaways
- Safety Checks: Use
and
to enforce multiple criteria (e.g., load + material checks). - Flexible Conditions: Use
or
to allow alternative compliance paths. - Inversion: Use
not
to exclude unsafe materials or invalid data.
Challenge Problem
Problem:
A retaining wall is stable if:
- The overturning moment ≤ 1.5 × resisting moment, AND
- The sliding resistance ≥ 1.2 × driving force, OR
- It includes a shear key AND the safety factor ≥ 1.0.
Write a program to determine stability.
Sample Input:
Overturning moment (kN·m): 1200
Resisting moment (kN·m): 2000
Sliding resistance (kN): 800
Driving force (kN): 700
Includes shear key? (yes/no): yes
Safety factor with shear key: 1.3
Sample Output:
Condition 1 (Overturning): 1200 ≤ 3000 → True
Condition 2 (Sliding): 800 ≥ 840 → False
Condition 3 (Shear Key + SF): True
Wall is STABLE: True
Hint:
condition1 = overturning_moment <= 1.5 * resisting_moment
condition2 = sliding_resistance >= 1.2 * driving_force
condition3 = has_shear_key and (safety_factor >= 1.0)
is_stable = (condition1 and condition2) or condition3