Learning Objectives
Understand how to easily search for letters or words inside text using Membership Operators.
Learn the difference between checking if things are equal versus checking if they are the exact same object using Identity Operators.
Discover how Python decides which math to do first using Operator Precedence (the rules of the road).
Membership Operators: Finding Hidden Text Imagine you are reading a long book and you want to know if the main character's name is mentioned on a specific page. You scan the page with your eyes, looking for that specific word hidden among all the other words.
Python does this exact same thing using Membership Operators. They allow you to test whether a specific value (like a letter or a word) is found hidden inside a larger sequence of text (a string).
There are two membership operators, and they are wonderfully simple English words:
in: Returns True if the value is found.
not in: Returns True if the value is not found.
Let's imagine you are building the backend for a massive email marketing platform. Before the system sends a campaign to a user's inbox, it includes a spam filter. The filter checks the email's subject line to see if it contains a suspicious, spammy word.
Here is how you would ask Python to check for that:
You can also use this for very simple checks, like ensuring an email address actually has an "@" symbol in it. If a user types "hello_world.com", you can check if "@" in user_input to quickly know if they made a typo!
Identity Operators: Twins vs. The Same Person This next concept trips up a lot of beginners, so let's use an analogy.
Imagine you see two identical red cars parked next to each other on the street. They are the same make, the same model, and the same color. If you ask, "Are these cars equal?", the answer is Yes.
But if you ask, "Are these cars the exact same physical vehicle?", the answer is No. You can drive one away while the other stays parked. They are two separate objects taking up two separate parking spaces.
In Python, we have already learned about the == operator. That operator checks if two values are equal (like asking if the cars look the same).
Identity Operators, however, check if two variables point to the exact same physical space in the computer's memory.
The two identity operators are:
is: Returns True if both variables point to the exact same memory space.
is not: Returns True if they point to different memory spaces.
As a beginner, the most common and powerful way you will use the is operator is when checking if a variable is entirely empty. In Python, a completely empty variable doesn't equal zero; instead, it is assigned a special keyword called None.
Imagine you are coding a mobile puzzle game where players try to clear a 9 by 9 scoring grid. When a brand new player opens the app for the very first time, they don't have a high score yet. It isn't zero; it simply doesn't exist yet.
Here is how you use the identity operator to check if a value is None:
Why not just use == for None?
You could write highest_score == None, and it would usually work. But in Python, None is a very special, one-of-a-kind object in the computer's memory. Because there is only ever one None object in existence, it is considered much better practice (and faster for the computer) to use is None to check its absolute identity!
Operator Precedence: The Rules of the Math Road Now, let's talk about traffic lights for math.
When you put multiple operators into a single line of code, Python needs to know which one to calculate first. This is called Operator Precedence.
If you remember taking algebra in school, you might remember the acronym BODMAS (Brackets, Orders, Division, Multiplication, Addition, Subtraction) or PEMDAS. Python follows these exact same mathematical rules!
Let's imagine you are developing a marketplace application connecting local home cooks with nearby customers. You need to calculate a customer's total bill. The cooked meal costs 3000, the delivery fee is 500, but the customer has a 50% off coupon for their entire order!
Look what happens if we don't use parentheses:
Wait, 3250? That is definitely not a 50% discount for the customer!
Because of operator precedence, Python did the multiplication before the addition. It multiplied the 500 delivery fee by 0.5 (which is 250), and then added the 3000 meal cost.
To fix this, we use Parentheses (). Parentheses have the absolute highest precedence in Python. Anything wrapped in parentheses gets calculated first, ignoring all other traffic lights.
Using parentheses is not only necessary for correct math, but it also makes your code much easier for other programmers to read because your exact intentions are obvious!
Lesson Summary You have officially added some of the most powerful logic tools to your programming toolbelt!
Membership Operators (in, not in): Quick, English-like keywords used to check if a value exists hidden inside a sequence, like searching for a specific spam keyword inside an email subject line.
Identity Operators (is, is not): Used to check if two variables are pointing to the exact same physical object in the computer's memory. As a beginner, you will mostly use this to check if a variable is None (completely empty).
Operator Precedence: Python's "order of operations." Multiplication and division happen before addition and subtraction. If you need to force a specific calculation to happen first, always wrap it in parentheses ().