←Previous page

06COMPARISONS

True or false?

Every if asks a question. The answer is always True or False. Here you learn to write those questions.

IN REAL LIFE

You compare things every day.

Who is taller? Do we have the same? Each question has a yes or no answer.

Who is taller?

My big sister is taller than me. True!

Same age?

My cousin and I are both 9. Same age? True!

More candy?

I have 3 candies and my friend has 5. Do I have more? False.

LOOK FIRST

A question with only two answers.

True or False

A Python question has only two answers: True or False. They start with a capital letter, like the yes-or-no box.

Compare two values

3 < 5 asks: is 3 less than 5? Python answers True.

One = or two?

One = puts a value in a box. Two == ask if two values are the same.

THE COMPARISON SIGNS

Six ways to compare.

SignIt asksExampleAnswer
==Are they the same?4 == 4True
!=Are they different?"cat" != "dog"True
<Is the left one smaller?7 < 2False
>Is the left one bigger?9 > 3True
<=Smaller, or the same?5 <= 5True
>=Bigger, or the same?6 >= 10False

A trick: < and > are like a hungry mouth. The open side always faces the bigger number.

JOIN QUESTIONS

and, or, not.

Sometimes one question is not enough. These three words help. Each one gets its own page next.

and: both must be True

sunny and warm is True only when sunny is True and warm is True.

or: one is enough

cake or ice_cream is True when at least one of them is True.

not: flip the answer

not True is False. not False is True.

RUN IT

Can PyBot play outside?

Guess first. Then run real Python and check.

1

Guess the result

battery = 50 sunny = True if battery > 30 and sunny: print("play outside") else: print("stay inside")

What will Python show?

true_or_false.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change True to False. Which path runs now? Then try battery = 10.

YOUR TURN

True or False?

Try each tiny question. A wrong answer becomes something to review.

0Completed
10Not tried
0Review
01

Sticker count

Ana has 4 stickers. Leo has 7 stickers.

Ana has more stickers than Leo. True or False?

02

Smaller or bigger?

3 < 5

What does Python answer?

03

The same?

7 == 8

What does Python answer?

04

Box or question?

score = 10 score == 10

Which line asks a question?

05

Different?

"cat" != "dog"

What does Python answer?

06

Both needed

has_ticket = True is_tall = False has_ticket and is_tall

What does Python answer?

07

One is enough

raining = False snowing = True raining or snowing

What does Python answer?

08

Flip it

not True

What does Python answer?

09

Fix PyBot's code

PyBot wants to ask a question, not store a value.

Goal output
Three stars!
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

A comparison is a question with a True or False answer.

if uses that answer to choose a path. Next: join two questions with and.