thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

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

All courses

Python Basics / 03.17

Understand Boolean values

Not started

Use Boolean values, comparisons and logical operators to represent a clear yes-or-no question.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

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.

Rob compares a score of 80 with a target of 80 using greater than or equal to and sees the Boolean True.
Rob’s comic strip · Python basics

ORIGINAL WALKTHROUGH · 10:41

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

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)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

main.py
main.pypython · example
# Code along with Rob: Boolean values
# Assign actual True and False values without quotes.
# Compare values below, at and above a target.
# Combine Boolean answers with and, or and not.
# Choose names that describe the question being answered.

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.

Name the question, store its Boolean answer
booleans.pypython · example
is_ready = True
has_file = False

print(is_ready)  # True
print(has_file)  # False
Open workspace to paste this example (opens in a new tab)

Comparisons 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.

At the boundary, the operator matters
booleans.pypython · example
score = 80
target = 80

print(score > target)  # False
print(score >= target)  # True
print(score == target)  # True
print(score != target)  # False
Open workspace to paste this example (opens in a new tab)

Combine 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.

Read both input answers before predicting the result
booleans.pypython · example
is_ready = True
has_file = False

print(is_ready and has_file)  # False
print(is_ready or has_file)  # True
print(not is_ready)  # False
Open workspace to paste this example (opens in a new tab)

A 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.

Recalculate after changing an input
booleans.pypython · example
score = 70
has_passed = score >= 80
score = 90

print(has_passed)  # False
has_passed = score >= 80
print(has_passed)  # True
Open workspace to paste this example (opens in a new tab)

Preview: 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.

A Boolean can control an indented instruction
booleans.pypython · example
score = 80
has_passed = score >= 80

if has_passed:
    print("Target reached")
Open workspace to paste this example (opens in a new tab)

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.

Expected output
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.

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.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

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)

Continue learning

ONE STEP FORWARD

Explain why exactly meeting a target should pass, then fix a comparison that excludes equality.