Skip to content

ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ

True, False, and comparisons

3 min read

Until now your programs have run every line, top to bottom, every time. Real programs choose: pay or decline, pass or fail, open or stay locked. Every choice starts with a question that has a yes-or-no answer, and by the end of this lesson you'll ask Python those questions and read its answers.

Every lesson in this module works fully in your browser.

A question with a yes-or-no answer

Read these three lines and predict all three answers before you reveal them.

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

print(5 > 3)
print(5 == 5.0)
print("5" == 5)

Python prints

True
True
False

5 is greater than 3, so the first is True. 5 and 5.0 are the same number, so the second is True. The third compares text with a number: "5" in quotes is text, and text is never equal to a number, so it's False.

A comparison is a question, and Python answers it with one of two values: True or False. They're written with a capital letter and no quotes. These two values have their own type, called a (bool for short).

has_ticket = 250 >= 200
print(has_ticket)
print(type(has_ticket))

In the editor, press Escape then Tab to move on.

A Boolean is a value like any other, so you can give it a name. Change 200 to 300 and run it again.

Six ways to compare

Python has six comparison operators:

OperatorQuestion it asks
==is equal to?
!=is not equal to?
>is greater than?
<is less than?
>=is greater than or equal to?
<=is less than or equal to?

Read == aloud as "is equal to?", with the question mark. It asks; it never changes anything. A single = is different: it gives a name a value ("gets"). Mixing them up is the most common mistake in this module, and Python catches it:

Remember lesson 1.5: input() always gives you text, even when the user types digits.

ጥያቄ

The user types 7, so input() returns "7". What is "7" == 7?

Comparing text

You can compare text too. == checks that two strings are exactly the same, and case counts: "Hana" == "hana" is False. What about greater and less than? Predict these.

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

print("Zebra" < "apple")
print("10" < "9")
Pick the output

Python prints

True
True

Python compares text one character at a time, from the left, and every uppercase letter comes before every lowercase one. "Z" comes before "a", so "Zebra" < "apple". In "10" < "9", the first characters decide it: "1" comes before "9", and the rest is never looked at.

It isn't dictionary order. Python compares character by character, and uppercase comes before lowercase. That's why "10" < "9" is True: they're compared as text, not as numbers. If you mean numbers, convert them first.

Ranges and decimals

To ask whether a value is between two others, write it the way you would in math:

age = 25
print(18 <= age < 60)
age = 60
print(18 <= age < 60)

In the editor, press Escape then Tab to move on.

18 <= age < 60 reads "18 is at most age, and age is less than 60". The second answer is False because 60 isn't less than 60. Change the second age to 59 and check.

One comparison surprises almost everyone.

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

print(0.1 + 0.2 == 0.3)
Pick the output

Python prints

False

0.1 and 0.2 can't be stored exactly in binary, so their sum is 0.30000000000000004, a tiny bit more than 0.3. The two aren't exactly equal.

You met this in lesson 1.2. Don't compare decimal results with ==. For money, count whole santim (10 + 20 == 30 is True), or round both sides first.

መልመጃ

Is the taxi within budget?

A ride costs a base fare plus a price per kilometre (these are example prices). Work out the fare for a 7 km ride and print whether it's within a 250 birr budget. Then try a 9 km ride.

Output
Fare: 240 birr
Within budget: True

Your code

base_fare = 100
price_per_km = 20
km = 7
budget = 250
# Work out the fare, then print it and whether it's within budget

In the editor, press Escape then Tab to move on.

መፍትሄውን አሳይ

This is one way to solve it, not the only one. If yours prints the same thing, it works.

base_fare = 100
price_per_km = 20
km = 7
budget = 250
fare = base_fare + price_per_km * km
print("Fare:", fare, "birr")
print("Within budget:", fare <= budget)

ዋና ዋና ነጥቦች

  • A comparison is a question; its answer is True or False, a Boolean (bool).
  • == asks "is equal to?"; a single = gives a name a value. Python rejects if x = 5:.
  • Text is never equal to a number: "7" == 7 is False. Convert input first.
  • Text compares character by character, and uppercase comes first: "10" < "9" is True.
  • 18 <= age < 60 checks a range. Don't compare decimal results with ==.