Logical Operators

These are used to test multiple conditions to produce a single Boolean value.

  • AND(&& or “and”): Returns true if both conditions are true. If either condition is false or if both are false, it returns false.
  • OR(“   ” or “or”): Returns true if at least one condition is true. If both conditions are false, it returns false
  • NOT(! or “not”): If the condition is false, then it returns true. If the operand is true, then it returns false.

More Hacks! (Both python and javascript)

Example 1: Temperature Check

Example 1: Going with a temperature example, use logical operators to test whether the temperature is comfortable or not.

Javascript Version:

let temperature = 43;

// Boolean expressions
let comfortable = temperature < 83 && temperature > 63;
let notComfortable = temperature < 63 || temperature > 83;

if (comfortable) {
    console.log("It is a comfortable temperature");
} else if (notComfortable) {
    console.log("It is not a comfortable temperature");
}

Python Version:

## Example 1: Temperature Check
temperature = 43 

# Boolean expressions
comfortable = temperature < 83 and temperature > 63
not_comfortable = temperature < 63 or temperature > 83

if comfortable: 
    print("It is a comfortable temperature")
elif not_comfortable:
    print("It is not a comfortable temperature")

Example #2: You want to test if someone is authorized to enter a building or not.

Javascript Version:

// Define user status
let isAuthorized = false;  // Change this to true to test authorized access

// Use NOT to determine access
let canEnter = !isAuthorized;

// Output the result
console.log(canEnter ? "Access granted!" : "Access denied!");

Python Version:

# Define user status
is_authorized = False  # Change this to True to test authorized access

# Use NOT to determine access
can_enter = not is_authorized

# Output the result
print("Access granted!" if can_enter else "Access denied!")

Homework Hacks

  1. Create a portion of a cookie clicker game that says you win or lose based on the number of cookies you have OR the number of workers you have. Use relational and logical operators to do this. Have fun!
  2. Create a truth table. A truth table typically lists all possible combinations of truth values (True and False) for a given number of variables and their corresponding outputs for various logical operations.

Grading

Use this rubric to grade. Ratio is 1-4 1: 0.25 2: 0.50 3: 0.75 4: 0.90

Name Weightage Grade
Hack 1 15%  
Hack 2 5%  
Hack 3 10%  
Homework 55%  

3.5 Booleans Quiz

Boolean Quiz