SypherTec
…
PART 2 · 01 OF 083 MIN READUPDATED 20 NOV 2026

Comparisons: Questions With a True or False Answer

Your tracker reports what you spent. It cannot tell you whether that is too much. This lesson adds a budget and asks Python the first question your program can answer: is the total over budget?

The concept

Here is your tracker at the end of Part 1. Run it:

WHERE YOU LEFT OFF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Every line it prints is a value it worked out. Now run this:

TWO QUESTIONS ABOUT TWO NUMBERS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Python did not print a number. It printed False, then True. The first line asked "is total greater than budget?" and the answer was no. The second asked "is total less than budget?" and the answer was yes.

Writing two values with > or < between them is a comparison. A comparison is a question, and Python answers it with True or False. Here are the others:

THE OTHER COMPARISONS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

== asks "are these equal?". != asks "are these different?". >= and <= include the equal case: 12500 >= 12500 is True.

An answer is a value like any other, so you can store it:

AN ANSWER STORED IN A VARIABLE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

True and False have a type of their own, called bool. Every comparison gives back a bool.

Text can be compared too. Run this:

COMPARING TEXT · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Two strings are equal only when every character matches. A capital letter or a space at the end is enough to make them different.

The syntax

over_budget = total > budget

Read it right to left. Python works out total > budget first, which gives True or False. Then = stores that answer under the name over_budget.

There are six comparison operators.

  • == equal to.
  • != not equal to.
  • < less than.
  • > greater than.
  • <= less than or equal to.
  • >= greater than or equal to.

In <= and >=, the equals sign always comes second. True and False start with a capital letter.

A minimal working example

main.py
total = 12500
budget = 15000
over_budget = total > budget
 
print(f"Budget: ₦{budget:,}")
print(f"Over budget: {over_budget}")

Output: Budget: ₦15,000, then Over budget: False. An f-string prints a bool the same way it prints any other value.

Common mistakes

You use one equals sign to compare. This trips up almost everyone. Run this:

ONE EQUALS SIGN · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
TypeError: 'total' is an invalid keyword argument for print()

One = stores a value. Python did not read your line as a question at all. It thought you were setting an option called total on print. To ask whether two values are equal, write ==.

You compare a number with text. Run this:

A NUMBER COMPARED WITH TEXT · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
TypeError: '>' not supported between instances of 'int' and 'str'

"15000" has quotes, so it is a string. Python will not say whether a number is bigger than some text. Convert the text with int() first, the way you did with your fuel receipt.

You write the operator the wrong way round. total =< budget gives SyntaxError: invalid syntax. The equals sign goes second: <=.

You write true in lowercase. Run this:

TRUE IN LOWERCASE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Lowercase true is not a value Python knows, so it treats it as a variable name. Nothing was assigned to that name. Read the end of the error: Python suggests 'True'.

Try it yourself

You will give your tracker a budget, then ask whether you went over it.

  1. Run the starting code below. Write down your total.
  2. Under the month line, add budget = 15000, or your own monthly budget in whole naira. At the end of the file, add print(f"Budget: ₦{budget:,}"). Run it. Confirm the last line shows your budget.
  3. Under the smallest line, add over_budget = total > budget. At the end of the file, add print(f"Over budget: {over_budget}"). Run it. Confirm the last line says False if your total is below your budget, and True if it is above. With the amounts given, it is False.
  4. Change your budget to 10000 and Run it. Confirm the answer flips. Then put your budget back.
  5. Break it on purpose: put quotes around your budget, for example budget = "15000". Run it. Confirm the console shows a TypeError that mentions '>', 'int' and 'str', pointing at your over_budget line.
  6. Fix it: remove the quotes. Run it. Confirm your report returns with its Over budget line.
TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Your tracker can now answer a question about your spending. It still prints the answer the same way every time, though: a bare True or False. The next lesson uses that answer to decide what to print.

QUIZ · 3 QUESTIONSCheck what stuckStart the quiz
PART 2 · 01 OF 08❮❯