Module 5: Comparisons & Booleans

Making logical checks and decisions in Python.

⚖️ Making Comparisons

Welcome to Module 5! AI systems constantly need to make decisions based on data. Is a detected object's confidence score above a threshold? Is a sensor reading different from the last one? Is the predicted category equal to 'spam'?

To do this, programs need to compare values. Python uses special operators for these comparisons, and the result is always a simple True or False. Let's explore!

↔️ The Comparison Toolkit

These symbols ask Python questions about values, always resulting in True or False.

==

Equal to?

Checks if two values are the same. (e.g., 5 == 5 is True)

!=

Not equal to?

Checks if two values are different. (e.g., 5 != 3 is True)

>

Greater than?

Checks if the left value is bigger than the right. (e.g., 10 > 5 is True)

<

Less than?

Checks if the left value is smaller than the right. (e.g., 3 < 5 is True)

>=

Greater than or equal to?

Checks if the left value is bigger than or the same as the right. (e.g., 5 >= 5 is True)

<=

Less than or equal to?

Checks if the left value is smaller than or the same as the right. (e.g., 3 <= 5 is True)

🚦 The Results: True or False

Comparisons don't return numbers or text; they return one of these two special values.

True

Represents a "yes" or affirmative result of a comparison. It's a distinct data type called bool.

False

Represents a "no" or negative result. Also part of the bool data type.

⚙️ Comparisons at Work

Let's see Python evaluate some comparisons using variables.

confidence_score = 0.85 detection_threshold = 0.9 items_detected = 5 is_confident = confidence_score >= detection_threshold # Is score high enough? found_something = items_detected != 0 # Did we find anything? is_single_item = items_detected == 1 # Exactly one item? print(f"Confident detection: {is_confident}") print(f"Found items: {found_something}") print(f"Single item found: {is_single_item}") print("Type of result:", type(is_confident))

We set up some variables relevant to an AI task (like object detection).

Then, we use comparison operators like >=Checks if 'confidence_score' is greater than or equal to 'detection_threshold'., !=Checks if 'items_detected' is not equal to 0., and ==Checks if 'items_detected' is exactly equal to 1. to ask questions.

Each comparison evaluates to either TrueThe result when the comparison condition is met. or FalseThe result when the comparison condition is *not* met.. We store these Boolean results in new variables (`is_confident`, `found_something`, `is_single_item`).

Finally, we print the results. Notice how `print(type(...))` confirms the result is of type This indicates the Boolean data type.. These True/False values are crucial for making decisions in code (which we'll see in the next modules!).

🧠 Quick Check!

Module 5 Theory Complete!

Fantastic! You now understand how Python compares values and the fundamental Boolean results (`True`/`False`). This is the bedrock of decision-making in code.
Test your skills in the Module 5 Practice Zone or challenge yourself with the Advanced Practice.