Code Bytes · "Intro to Programming" · Episode 8 · Decisions and loops
Comparison and logic
Every decision in code is a yes or no question, and Python answers it with True or False. Comparisons like >, < and == ask the question, and the three little words and, or and not combine and flip the answers.
Every decision is a yes or no question
Last episode, an if statement checked something and picked which lines to run. Underneath, every decision comes down to a yes or no question. In Python, yes is written True and no is written False, both with a capital letter. These two values have their own type, called a bool (short for Boolean).
Comparisons ask the question
A comparison puts two values side by side and answers with True or False:
>means greater than<means less than==means are these the same?
Watch the equals signs. One = puts a value in a box: age = 15 stores 15 in a variable called age. Two == compare: age == 18 asks whether the box holds 18.
``python age = 15 print(age > 12) print(age == 18) ``
`` True False ``
15 is greater than 12, so the first line prints True. 15 is not 18, so the second prints False.
and: both sides must be True
Sometimes one question isn't enough. Is someone a teenager? They must be older than 12 and younger than 18. The word and joins two questions, and the result is True only when both sides are True:
``python age = 15 print(age > 12 and age < 18) ``
`` True ``
age > 12 is True and age < 18 is True, so the whole thing is True. If either side were False, the answer would be False.
or: one side is enough, and not flips
or is easier to please: it is True if at least one side is True.
``python age = 15 print(age < 5 or age > 65) ``
`` False ``
15 is not under 5 and not over 65. Neither side is True, so the answer is False.
The third word, not, works on a single answer and flips it:
``python print(not True) ``
`` False ``
not False gives True, the other way round.
Try it yourself
Open any free online Python editor (search "online Python") and run:
``python temp = 30 print(temp > 25 or temp < 0) print(temp > 25 and temp < 28) print(not temp == 30) ``
You should see:
`` True False False ``
Before you run it, cover the output and predict each line. Then change temp to -5 and predict again.
Good to know
- Every snippet on this page was run with Python 3 and printed exactly what is shown.
- There are three more comparisons:
>=(greater than or equal to),<=(less than or equal to) and!=(not equal to). Withage = 15,age >= 15isTrue,age <= 14isFalseandage != 18isTrue. - Mixing up
=and==is one of the most common beginner mistakes. Writingif age = 18:stops Python with aSyntaxError(it even suggests you meant==). - Python works out comparisons first, then
not, thenand, thenor. Soage > 12 and age < 18means(age > 12) and (age < 18). When in doubt, you can add brackets yourself to make it clear. andandorstop early once the answer is known (if the left side of anandisFalse, the right side isn't even checked). With values that aren'tTrueorFalse, they give back one of the values instead. You won't need either detail yet.- These three words are the heart of Boolean logic, which comes back later in the course with truth tables.
Answer to the comment question
What does this print?
``python x = 7 print(x > 5 and not x == 8) ``
It prints True. Python checks the comparisons first: x > 5 is True (7 is greater than 5), and x == 8 is False (7 is not 8). not flips that False to True. Then and joins the two sides: True and True is True.
Test your knowledge
Quick quiz: Comparison and logic
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Python Software Foundation, "Built-in Types": Comparisons: >, <, ==, !=, >=, <= compare two values and give a bool result
- Python Software Foundation, "Built-in Types": Boolean Operations, and, or, not: what and, or and not do
- Python Software Foundation, "Expressions": Operator precedence: comparisons before not, not before and, and before or
- Python Software Foundation, "Data Structures": More on Conditions: combining comparisons with and, or and not
Video transcript
Which three little words make every decision in code?
Every decision needs a yes or no question. In code, yes is True and no is False.
Comparisons ask those questions. Greater than, less than, and double equals, which asks: are these the same? One equals sign puts a value in a box. Two equals signs compare.
Age equals fifteen. Print age greater than twelve: True. Print age double equals eighteen: False.
Now the three words. And is True only when both sides are True. Age greater than twelve and less than eighteen? True. A teenager.
Or needs only one side. Age under five or over sixty-five? Neither, so False.
And not flips the answer. Not True gives False.
What does this print? Comment your answer below!
That's your byte.
More from "Intro to Programming"
- "Intro to Programming" EP5Data types
- "Intro to Programming" EP6Strings
- "Intro to Programming" EP10While loops
- "Intro to Programming" EP11Infinite loops