Ask yes-or-no questions about your data with comparison operators and store the answers as True or False.
About 30 minBeginner4 exercisesFree
You will be able to
Explain what the boolean values True and False are and where they come from
Compare numbers with ==, !=, <, >, <= and >=
Compare strings and predict how case and spaces affect the result
Use bool() to find out whether a value counts as true or false
Programs constantly answer yes-or-no questions. Does this order qualify for free shipping? Is the stock level below the reorder point? Did the customer type the same code twice? Python answers each of these questions with one of two special values: True or False.
These two values have their own type, called bool (short for boolean, after the mathematician George Boole). In this lesson you'll produce booleans with comparisons and store them in variables. In the next track, Conditions, you'll use them to make your program choose what to do.
Try it: Change order_total to 200 exactly. Is it still free shipping? Then change >= to > and compare.
Python works out the right-hand side first: order_total >= threshold becomes True, and that value is stored in free_shipping. Comparisons have lower precedence than arithmetic, so in price * qty > 100 the multiplication happens first and the comparison last.
Integers and floats compare by value: 10 == 10.0 is True.
Comparing strings
Strings can be compared too. == is True only when both strings contain exactly the same characters — same case, same spaces. < and > compare strings character by character in alphabetical order based on character codes, where every uppercase letter comes before every lowercase letter.
That last pair is the source of a very common bug. input() always gives you text, so comparing two typed numbers without converting them compares them alphabetically. Convert with int() or float() first.
bool(): does this value count as true?
Every value in Python can be asked "are you true or false?" with bool(). The rule is short: empty and zero values are false, everything else is true.
False: 0, 0.0, "" (the empty string), and None (which you'll meet later in this track)
True: any other number, and any non-empty string — including " ", "0" and "False"
Which values count as false?
print(bool(0),bool(17),bool(-3.5))print(bool(""),bool("hello"))print(bool(" "),bool("0"),bool("False"))
note =" "
has_note =bool(note.strip())print("Customer left a note:", has_note)
Try it: Change note to " Leave at the door " and run it again.
Guided practice · ●difficulty 1 of 5 · about 4 min
Free shipping check
An online shop gives free shipping on orders of 200 SAR or more. Write a program that:
reads the order total with input() (no prompt text — the tests type the values for you) and converts it with float(),
stores the answer to "is this free shipping?" in a variable named free_shipping (a boolean),
prints it like this:
Free shipping: True
For an input of 150.75 the program prints Free shipping: False. An order of exactly 200 qualifies.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Challenge · ●●difficulty 2 of 5 · about 6 min
Can we fill this order?
A warehouse system reads two whole numbers, one per line: the number of units in stock, then the number of units ordered. Use input() with no prompt text and convert each with int().
Create two boolean variables:
can_fill — True when there are at least as many units in stock as were ordered,
reorder — True when the stock left after sending the order would be below 10 units.
Then print them in this format. For stock 40 and order 35:
Can fill order: True
Reorder needed: True
For stock 40 and order 12, 28 units would be left, so the second line is Reorder needed: False.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Edge cases · ●●difficulty 2 of 5 · about 6 min
Do the voucher codes match?
A cashier types a voucher code twice to make sure it was entered correctly. People are careless with capital letters and often add stray spaces, so " sale25" and "SALE25 " should count as the same code.
Read the two codes (one per line, input() with no prompt text). Store True or False in a variable named codes_match, ignoring case and spaces at the start and end, then print:
Codes match: True
Spaces inside a code still matter: "SALE 25" is not the same as "SALE25".
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Debugging · ●●difficulty 2 of 5 · about 5 min
Fix the temperature alarm
A cold-storage room logs two temperature readings, one per line: the current reading and the limit. The program should report whether the current reading is above the limit.
Staff noticed something odd: with a current reading of 9 and a limit of 10 it says the room is too warm, and with 12 and 8 it says it is fine. Find and fix the bug. Keep the variable name too_warm and the output format:
Too warm: False
Readings can have decimals, such as 7.5.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Summary
True and False are the two values of the bool type; write them capitalised and without quotes.
Comparison operators (==, !=, <, >, <=, >=) produce a boolean you can store in a variable.
Arithmetic runs before comparison, and an int equals a float with the same value.
String comparison is exact and character by character: case, spaces and digits-as-text all matter.
bool() treats zero and empty values as False and everything else as True.