Intro to Computer Science / Unit 5: Conditionals and Boolean Logic
5.3 Logical Operators
Notes
Combine conditions with and, or and not.
A and Bis True only if both are True.A or Bis True if at least one is True.not Aflips True to False and False to True.- Example:
age >= 13 and age <= 18is True for ages 13 through 18. - Evaluation order:
not, thenand, thenor. Use parentheses to be clear. - Example: with
x = 5,x > 2 and x < 4isFalse, butx > 2 or x < 4isTrue.
Practice
What does
print(3 > 1 and 2 > 5) display?The second part is False, and and needs both parts True.
What does
print(not (4 == 4)) display?4 == 4 is True, and not flips it to False.
Which condition is True when a number n is between 10 and 20, inclusive?
Both bounds must hold, and inclusive means using >= and <=. The or version is True for every number.
Ready for more? Take the Unit 5 test.