True or False: Asking Yes/No Questions


To make decisions (next lesson), a computer first has to answer yes/no questions: Is this bigger than that? Are these equal? Python has a special pair of values for the answers: True and False. They’re called booleans, and today you’ll learn to make them.

Comparisons make True or False

Ask Python to compare two things and it answers True or False:

SignMeans
==is equal to
!=is not equal to
>greater than
<less than
>=greater than or equal
<=less than or equal
 

That double equals == is important: a single = stores a value (age = 12), while double == asks “are these equal?” Mixing them up is a classic bug.

Try it 🎯

  1. Print whether 100 is less than or equal to 100.
  2. Make a variable age = 12 and print age >= 13 (are they a teenager?).

Comparing words too

Comparisons work on strings — handy for checking answers:

 

Combining questions: and, or, not

Real questions often have parts. Python uses plain words:

  • and — True only if both sides are True.
  • or — True if either side is True.
  • not — flips True to False and back.
 

The first asks “is the age between 12 and 20?” Both parts are True, so the answer is True.

Try it 🎯

A ride needs you to be at least 10 and no taller than 200cm. With age = 11 and height = 150, write the check: age >= 10 and height <= 200.

Predict it 🔮

What does this print? Watch the single vs double equals. (Hint: one of these lines is a question, the answer is True/False.)

 

(It prints True. The == asks a question; x really is 8, so the answer is True.)

Fix the bug 🐞

This should check whether the user guessed the secret number 7, but it crashes with a syntax error. Look at the equals sign in the comparison:

 

(A comparison needs double equals. Change guess = secret to guess == secret.)

Your mission 🚀

Ask the user for a number, then print True or False for whether it’s a “big” number — say, greater than 100:

 

What you learned today

  • True and False are booleans — the computer’s yes/no answers.
  • Comparisons (==, !=, >, <, >=, <=) produce booleans.
  • = stores; == asks. Don’t mix them up!
  • and, or, not combine yes/no questions.

Next time, you put these to work: if lets your program do different things depending on whether a question is True. 🔀

Comments