Boolean Expressions and Comparison Operators
Expressions that come out true or false: comparisons between values, and the way and, or and not combine them.
What a learner can do afterwards
- Write a condition that is true only inside a range of values
- Predict the value of a condition that joins two comparisons with and or or
- Rewrite a condition using not without changing what it means
1 · Read
Programs ask questions that answer true or false, like score > 10 or temperature < 32. A snow jacket warning runs when temperature < 32 is true, and stays quiet when it is false. Every decision in code rests on one of these answers.
To catch numbers from 10 to 20 you write n >= 10 and n <= 20. The word and needs both sides true, while or needs only one side true. With n at 15 the whole test is true, and with n at 25 the second half fails so the test is false.
The word not flips an answer, and two nots cancel back out. So not temp < 0 means exactly the same as temp >= 0. Swapping an operator plus not keeps the meaning while making the code easier to read.
Read each condition aloud with real values plugged in. One equals sign stores a value and two compare values. In Python, if score = 10: is a syntax error, so nothing runs until you write ==. The quiet slip is the other way round: score == 10 alone on a line compares and stores nothing.
Comparisons answer true or false, and and, or and not join them into bigger tests.
2 · Watch
به انگلیسیTake it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.