Learn how computers make decisions using True and False values.
How does a self-driving car know exactly when to stop at a red light? It isn't guessing—it's making a lightning-fast 'True' or 'False' decision based on the world around it.
In Python, every big decision starts with a tiny choice: True or False. These are called Booleans, named after the mathematician George Boole. Think of a Boolean like a light switch; it can only be in one of two states. In computer science, we use these to control the flow of a program. If a condition is `True`, the computer does one thing; if it is `False`, it does something else. For example, a video game might check `is_game_over`. If that variable is `True`, the 'Game Over' screen appears. Without Booleans, computers wouldn't be able to 'think' or react to different situations.
Quick Check
If a computer is checking if a user's password is correct, what data type would the result of that check be?
Answer
A Boolean (True if it matches, False if it doesn't).
How do we actually get a `True` or `False` value? We ask the computer a question using Comparison Operators. These symbols compare two values and spit out a Boolean result. The most common ones are: - `==` (Equal to): Checks if two things are the same. Note the double equals! - `!=` (Not equal to): Checks if two things are different. - `>` and `<`: Greater than and less than. - `>=` and `<=`: Greater than or equal to, and less than or equal to.
In Python, writing is like asking the computer, 'Is five greater than three?' The computer will evaluate this and return `True`.
Let's see how a game checks for a new high score. 1. Current High Score: 2. Player's Score: 3. The computer evaluates: 4. The result is `True`.
Quick Check
What is the result of the expression: ?
Answer
False
Sometimes, we need to evaluate expressions that are slightly more complex. This involves comparing variables to values or even comparing the results of two different math problems. When Python sees an expression like , it follows the order of operations: it solves the math first () and then performs the comparison (). This allows us to create dynamic logic, such as checking if a player's health is below a certain percentage: .
A player wants to pick up an item. The game checks if they have room. 1. `max_items` = 2. `current_items` = 3. Logic: `current_items + 1 <= max_items` 4. Calculation: is . 5. Comparison: is `True`. The player can pick it up!
The `not` operator flips a Boolean to its opposite. 1. Let `is_sunny = True`. 2. The expression `not is_sunny` evaluates to `False`. 3. Challenge: Evaluate `not (5 > 10)`. 4. First, is `False`. 5. Then, `not False` becomes `True`.
Which operator is used to check if two values are NOT equal?
What is the result of the expression: ?
The expression evaluates to True.
Review Tomorrow
In 24 hours, try to explain to a friend why is True but would cause an error in a comparison.
Practice Activity
Write down three real-world 'True/False' questions a smart fridge might need to ask (e.g., 'Is the door open?').