Comparing Things (Is This Bigger, Smaller, the Same)
Okay so here's the thing. You've got if, you've got else, you've got elif. What you don't have yet is a good way to actually compare two things. Right now your programs can only really check "did the user type the exact word yes." That's fine, but it's not going to get you very far. Real programs need to ask questions like: is this number bigger than that one? Is the cart total over budget? Did they type the right password, or close enough, or completely wrong?
That's what comparison operators are for. And I promise this part is super simple once it clicks.
The operators
Python gives you six of these:
``python == # equal to != # not equal to > # greater than < # less than >= # greater than or equal to <= # less than or equal to ``
The one that trips everybody up first is == versus =. One equals sign means "set this variable to this value." Two equals signs means "check if these two things are equal." Mix those up and Python will either yell at you or, worse, quietly do something you didn't mean. Ask me how I know. Actually don't, we covered variable-naming disasters already, let's not pile on.
Try typing this yourself, don't copy-paste it, your fingers need to feel the two equals signs to remember them:
``python age = 15 print(age >= 16) ``
Run it. Python prints False. That's it. That's the whole trick. A comparison isn't a question that gets answered in words, it spits out one of exactly two things: True or False. Those are special values in Python, not just text that says true, and you'll see them a lot from here on out.
Putting it inside an if statement
This is where it gets useful. You already know how if works. Now just hand it a comparison instead of a plain yes/no check:
```python age = 15
if age >= 16: print("You can drive.") else: print("Not yet.") ```
Super small step, but that's a real, working piece of logic. The computer is comparing two numbers and making a decision based on it. Nobody typed "yes" or "no" anywhere.
Try it on your own machine
- Make a variable called
temperatureand set it to some number. - Write an
if/elif/elsethat prints "put on a coat" if it's under 40, "kind of nice" if it's between 40 and 70, and "too hot, stay inside" if it's over 90. (Elevation up here means our weather swings around more than people expect, so this one's not hypothetical for me.) - Run it three separate times, changing the temperature each time, so you actually see all three paths fire.
That third step matters. A lot of folks write the if/elif/else, run it once, see it work, and move on. Then they never find out the elif branch has a typo in it because they never triggered it. Test every path. Every single time.
An opinion, since we're here
People assume you need to be good at math to write this kind of code. You don't. This whole lesson is basically English with symbols. "Is this bigger than that." "Is this the same as that." You're not doing algebra, you're asking a question and getting a straight answer back. If you can compare grocery prices in your head to figure out which size is the better deal, you can write a comparison operator. That's genuinely most of it.
Why this actually matters
I had a student a while back, first time she'd ever touched code, and she got a comparison working inside an if statement, temperature thing, actually pretty close to the exact exercise above. Ran it, saw "put on a coat" print out for a number she'd typed in herself. She gasped. Out loud. In a room full of adults staring at text files. Loudest reaction I've ever gotten from a print statement, and I've been doing this a while.
That's the whole appeal, right there. It's not that comparisons are hard. It's that the moment you see the computer make a real decision based on a number you handed it, something clicks. It stops being "typing words at a screen" and starts being an actual little decision-maker that you built. That's the heist. Every time.
Before next time
Play around with all six comparison operators on numbers you make up, just to see what prints. Try comparing two variables to each other instead of a variable to a plain number, that's where this starts feeling like something real.