03.17 · Python basics / Watch · 17 min
Ask a question that returns True or False
A comparison turns a question about your values into a Boolean answer. Check the boundary as well as an obvious passing example.
Activity 17 of 19 in Python basics
Python Basics / 03.17
Understand Boolean values
Not started
Use Boolean values, comparisons and logical operators to represent a clear yes-or-no question.
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

ORIGINAL WALKTHROUGH · 10:41
Watch with Rob.
Pause whenever you want to try a change. You can return to the lesson at any time.
You are signed in, but video access needs attention. Check your account access. Your saved work is kept.
Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.
Open video & code editor (opens in a new tab)True and False are values, not quoted words
A Boolean value is either True or False. Python capitalizes the first letter. The unquoted value False is different from the string "False". Quotes make text, even when that text looks like a Boolean word.
In the example, is_ready stores True and has_file stores False. A useful name tells the reader what that answer means. A prefix is a naming convention, not a special Python feature that forces a value to be Boolean.
is_ready = True
has_file = False
print(is_ready) # True
print(has_file) # FalseComparisons produce Boolean answers
Use > for greater than, < for less than, >= for greater than or equal to, <= for less than or equal to, == for equal values and != for unequal values. Assignment uses one equals sign; an equality comparison uses two.
With score and target both 80, score > target is False while score >= target is True. If reaching the target is enough, equality must be included. Test a value below, at and above the boundary when you experiment.
score = 80
target = 80
print(score > target) # False
print(score >= target) # True
print(score == target) # True
print(score != target) # FalseCombine Boolean answers with and, or and not
With Boolean operands, and is True only when both operands are True. or is True when at least one operand is True. not reverses the truth value. The example prints False, True and False.
Python and/or can return an operand rather than a Boolean when you give them other kinds of values. Keep the inputs in these beginner examples as actual Booleans; do not assume every and/or expression creates a bool.
is_ready = True
has_file = False
print(is_ready and has_file) # False
print(is_ready or has_file) # True
print(not is_ready) # FalseA stored answer reflects the time you calculated it
A comparison runs when Python evaluates that expression. If you later change an input, an existing Boolean variable does not automatically recalculate. Assign the comparison again when you need an updated answer.
Here has_passed stays False after score changes, until the comparison is assigned again. This is the same stored-value idea you saw with formatted strings.
score = 70
has_passed = score >= 80
score = 90
print(has_passed) # False
has_passed = score >= 80
print(has_passed) # TruePreview: use the answer to choose whether code runs
An if statement can use a Boolean answer to decide whether to run an indented block. In this preview, has_passed is True, so the message appears. We will study control flow and indentation in their own section.
For the next practice, concentrate on the comparison itself. Keep the starting values, include equality in the passing rule and store the actual Boolean result rather than a quoted word.
- Python documentation: comparisons (opens in a new tab)
- Python documentation: Boolean operations (opens in a new tab)
score = 80
has_passed = score >= 80
if has_passed:
print("Target reached")Build it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)4 steps · Saved work · Instant feedback
YOUR TURN
Include equality in the passing rule
Keep points assigned to 80 and required_points assigned to 80. Change has_passed so it checks whether points is greater than or equal to required_points. Keep print(has_passed). To pass: both input values must remain 80, has_passed must hold the Boolean True rather than the string "True", and the program must print True exactly once. After passing, try 79 and 81 points to check both sides of the boundary.
True
Keep the starting inputs. Print these lines in this order, without extra output.
- points still holds 80
- required_points still holds 80
- has_passed holds the Boolean True
Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
Include the boundary
A strict greater-than comparison excludes equality. Use the comparison that also accepts exactly the required value.
Store the answer to the comparison
Use the expression to calculate has_passed. Quoting True would create text instead of the Boolean result.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
The >= comparison is True when points equals or exceeds required_points. At 80 versus 80 it includes the passing boundary. Try a lower and a higher value to verify the rule. The tests check stored values and output, not a particular source spelling.
points = 80
required_points = 80
has_passed = points >= required_points
print(has_passed)Ready to try it?
Practice in your browser and see your results.
Progress details
Have a question?
Ask the Code Zone community.
Ask a question in The Code Zone (opens in a new tab)