Comparison & Logical Operators

Acadestine

Learning Objectives

    Understand how computers make decisions using True or False values.

    Learn to ask the computer questions using Comparison Operators.

    Discover how to combine multiple questions at once using Logical Operators.

Making Decisions in Code So far, we have learned how to store data (variables) and how to do math with that data (arithmetic operators). But a program that only does math isn't very smart.

Real software needs to make decisions.

If a user types the correct password, let them in.

If a player's score reaches 1000, they level up.

If a customer's cart total is over $50, give them free shipping.

To make these decisions, the computer needs a way to ask questions and get a simple "Yes" or "No" answer. In programming, we call "Yes" and "No" by different names: True and False.

We call these True or False answers Booleans.

Whenever we want the computer to make a decision, we use special operators to ask questions that result in a Boolean (True or False) answer.

Comparison Operators: Asking Questions Comparison Operators let us compare two values. They are exactly what they sound like: they compare the left side to the right side and answer with True or False.

Here are the most common comparison operators you will use:

Equal to (==): Checks if two values are exactly the same. (Notice it is TWO equals signs! One equals sign = assigns a value. Two equals signs == asks a question).

Not equal to (!=): Checks if two values are different.

Greater than (>): Checks if the left side is bigger.

Less than (<): Checks if the left side is smaller.

Greater than or equal to (>=)

Less than or equal to (<=)

Imagine you have built a digital puzzle game played on a 9 by 9 scoring grid. You need to write logic that checks if the player's current score is higher than the all-time high score.

Here is how you would ask Python that question:

Console

        

You can also use comparison operators on text (strings). Let's see if a user has entered the correct admin name:

Console

        
The Biggest Beginner Mistake: = vs ==

This trips up almost every new programmer! Remember: = means "Make it so." (e.g., score = 10 means "Make the score 10"). == means "Is it so?" (e.g., score == 10 means "Is the score currently 10?").

Logical Operators: Combining Questions Sometimes, asking just one question isn't enough.

Imagine you have built a massive email marketing platform. Before the system sends out an email campaign to a contact, it needs to verify two things:

Does this contact have a valid email address?

Have they explicitly subscribed to receive emails?

You need both of those things to be True. This is where Logical Operators come in. They allow you to combine multiple Boolean expressions together.

Python has three logical operators, and they are written in plain English:

  1. and If you use and, both sides must be True for the final result to be True. If even one side is False, the whole thing becomes False.(e.g., "You must have a valid email AND be subscribed.")

  2. or If you use or, only one side needs to be True for the final result to be True. (e.g., "To get a discount, you must be a VIP member OR have a coupon code.")

  3. not This one simply flips the answer. If something is True, not makes it False. (e.g., "If the user is NOT logged in, show them the login screen.")

Let's test this in our sandbox using our email marketing scenario:

Console

        

Try changing is_subscribed to True in the sandbox above and run it again to see how the logic changes!

Lesson Summary You just learned how to give your programs a brain! Let's review the key takeaways:

Booleans: Data that can only be one of two values: True or False.

Comparison Operators: Symbols used to ask questions about data (==, !=, >, <, >=, <=). They always result in a Boolean answer.

One vs. Two Equals: A single = assigns a value. Double == checks if two values are equal.

Logical Operators: The words and, or, and not. They are used to combine multiple comparison questions together to form more complex decision-making logic.