True or false values
Ask a yes-or-no question about two values and keep the answer. About 7 minutes.
Questions with yes-or-no answers
Not every value is a number. Some answer a question with just two possibilities: true or false. We call these booleans.
You get one by comparing two values. Is the age at least 18? Is the price more than the money in your pocket? The comparison words are: bigger than, smaller than, at least, at most, the same as, and different from. Each one gives back true or false.
age = 20 adult = age >= 18 # True
- money
- 50
- price
- 30
money holds 50 and price holds 30.
Keep the answer
A true-or-false answer can sit in a box like any other value. You can store it, pass it on, or give it back as the result of your function.
One warning: a single equals sign puts a value into a box. To ask whether two values are the same, you use two equals signs. Mixing them up is one of the most common beginner mistakes, so read each line carefully.
enough = money >= price same = money == price
money is 20 and price is 30. What does the question money >= price give back?
- Afalse
- Btrue
- C10
Show the answer
false. 20 is not at least 30, so the answer is false.
Can we afford it?
You have some money and want to buy something that costs a given price. Return true if you can afford it (you have at least the price), and false if not.
money = 50, price = 30 → true
0 ≤ money, price ≤ 1,000,000