Fall 2024 - P5
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.5 Logical Operators
Learn Logical Operators in Booleans
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 condition is true, then it returns false.
More Hacks! (Both python and javascript)
Popcorn Hack 2: 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")
Popcorn Hack 3: 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!")
DeMorgan’s Law
De Morgan’s Laws Explained:
Imagine you have two statements or conditions, like:
A: “It’s raining.” B: “I can play outside.”
First Law:
¬(A∧B)=¬A∨¬B
What it Means: If I say, “It’s not true that it’s both raining and I can play outside,” I’m saying at least one of these must be false. Translation: This means either “It’s not raining” or “I can’t play outside.” So if I don’t want both things to happen together, it means one of them isn’t happening.
Second Law:
¬(A∨B)=¬A∧¬B What it Means: If I say, “It’s not true that it’s either raining or I can play outside,” that means both must be false. Translation: This means “It’s not raining” and “I can’t play outside.” So if I don’t want either of those situations to happen, then both must not be happening.
Summary In simple terms:
First Law: The statement “not (A and B)” is equivalent to saying “not A or not B,” meaning at least one of the two must be false.
Second Law: The statement “not (A or B)” is equivalent to saying “not A and not B,” meaning both must be false.
These laws help us understand how negation (saying “not”) works with AND and OR in logic!
Homework Hacks
- All popcorn hacks (1-3) in both Javascript and Python. If you copy paste from the examples, we WILL make you take a (different, much harder) quiz and grade it.
- OPTIONAL: Take this quiz(Use school email): Quiz
- 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. Steps to Create a Truth Table 1. Identify the Variables: You would have 2 variables, A and B, representing True and False. 2. Determine the Number of Rows: The number of rows is 2^n where n is the number of variables, so 4 3. Write out all combinations of True (T) and False (F) for the variables. 4. Add columns for Each Logical Expression: 5. Create additional columns for the logical expressions you want to evaluate. 6. Calculate the Results: Evaluate the logical expressions for each combination of variable values and fill in the table.
Submit screenshots of your working code at this link: Submit
Truth table example:
A | B | A AND B | A OR B | |
---|---|---|---|---|
0 | 0 | 0 | 0 | |
0 | 1 | 0 | 1 | |
1 | 0 | 0 | 1 | |
1 | 1 | 1 | 1 |
Grading
Use this rubric to grade. Ratio is 1-4
** 4+ - top work, something beyond what was required, 1 to 2 per class (95%)
** 4 - performed 98% to 100% of requirements, majority should be .9 (89% to 92%)
** 3 - missing some key elements, 80% to 85%
** 2 - missing many to most key elements, 70%
** 1 - no work, 55%
** 0 - copying or not understand work performed, 0%
Name | Weightage | Grade |
---|---|---|
Hack 1 | 15% | |
Hack 2 | 15% | |
Hack 3 | 15% | |
Homework | 55% |