True, False and comparisons

Making Decisions, lesson 1 of 5

🔀 Making Decisions, lesson 1 of 5

True, False and comparisons

Ask yes/no questions about values with comparison operators.

8 min

1 exercise

1 quiz

0/2 solved

Getting Python ready… examples can run in a moment.

To make decisions, a program needs to ask yes/no questions. The answers are booleans (bool): either True or False (capital T and F, no quotes).

Comparison operators produce booleans:

Operator Meaning
== equal to
!= not equal to
< / > less / greater than
<= / >= less / greater than or equal
Example

Asking questions

Change age to 18 and predict each answer before running.

Quiz

What does this print?

You can compare strings too. == checks for exactly the same text (case matters), and < / > compare alphabetically, where all uppercase letters come before lowercase ones. Numbers compare by value, so 5 == 5.0 is True.

Example

Comparing text

Lowercasing both sides is a common trick for case-insensitive comparisons.

Exercise

Can I vote?

Set can_vote to a comparison that is True when age is 18 or more. Then try changing age to 16 and running again.