Doing Simple Math Without Being Good at Math
Okay so here's the thing. This lesson name is basically a disclaimer, because every single time I mention "math" in this class somebody's shoulders go up around their ears. So let's get it out of the way right now.
You do not need to be good at math to do this. I mean it. I am not good at math. I got a C in high school algebra and I have never once, in years of writing scripts for actual work, needed anything past what you learned around sixth grade. Adding things up. Multiplying things. Maybe dividing things if I'm feeling spicy. That's it. That's the whole "coding is math" myth, mostly stubbornness and reading error messages carefully, dressed up to sound scarier than it is.
What Python Can Do With Numbers
You already made variables last lesson. Now we're going to do stuff with them.
``python photos_taken = 400 photos_deleted = 12 photos_kept = photos_taken - photos_deleted print(photos_kept) ``
That's it. That's a whole program. Type it yourself, don't copy-paste it, your fingers need to feel the syntax go by or it doesn't stick the same way. Run it. It should spit out 388.
The symbols Python uses are mostly what you'd expect:
+adds-subtracts*multiplies (not an x, a star)/divides%gives you the leftover bit after dividing, which sounds useless until suddenly it's the most useful thing in your whole program
That last one, the percent sign, is called "modulo" if you want the fancy word. I mostly just call it "the remainder thing" because that's what it does.
Try This At Home
Open your editor. Make a new file. Give it a real name, something like practice_math.py, not untitled3 for the love of everything.
Type this out:
``python mileage_start = 84213 mileage_end = 84509 miles_driven = mileage_end - mileage_start gas_used = 11.2 mpg = miles_driven / gas_used print(mpg) ``
Run it. You just calculated gas mileage. Congrats, you did math, and it didn't feel like math class because nobody made you show your work on paper first.
Now break it on purpose. Change gas_used to 0 and run it again. Watch what happens. Python's going to yell at you about dividing by zero, and that's fine, that's Python being honest with you, not the program hating you personally. Well. Some days it does feel personal. We'll talk about that.
A Small Warning About Order
Python does math in the order you learned in school, multiplication and division before addition and subtraction, unless you tell it otherwise with parentheses. This trips people up constantly.
``python total = 10 + 5 * 2 print(total) ``
That prints 20, not 30, because Python multiplies first. If you wanted 30, you write:
``python total = (10 + 5) * 2 ``
Parentheses are cheap. Use them any time you're not a hundred percent sure what order things will happen in. I use them even when I'm pretty sure, honestly, because "pretty sure" has burned me before.
Okay So Here's a Story About Loops and Math Going Wrong
I want to tell you about the night my old laptop, the Subaru of laptops, bless it, sat there screaming its fan at me for twenty straight minutes.
I'd written a loop that was supposed to count up from 1 to 10 and print each number. Simple. Should take a tenth of a second. Instead my screen filled up with numbers scrolling by so fast I couldn't read them, and the fan kicked into what I can only describe as "takeoff mode."
I'd forgotten to actually change my counter variable inside the loop. So the condition that was supposed to stop it never got closer to being true. It just ran forever, over and over, same starting number every time, never getting anywhere. Very expensive way to do nothing.
The fix, when I finally saw it, was Ctrl-C. Hold that combo down in your terminal and it slams the brakes on whatever's running. That key combo has saved me probably a hundred times since. If your program ever seems to be running forever and your fan starts sounding like it's training for a marathon, that's your escape hatch. Don't panic, don't unplug your computer, just Ctrl-C out of it.
We'll get into loops properly in a couple lessons, but I wanted you to have that keyboard shortcut in your back pocket now, before you need it in a panic at 11pm like I did.
The Actual Point of This Lesson
Math in code is mostly just telling the computer to do arithmetic you already know how to do, and letting it handle numbers way faster and more reliably than you could by hand. That's the whole superpower. It's not clever, it's not hard, it's just fast and it doesn't get tired.
If a math problem in this class ever looks intimidating, break it into stupider, smaller pieces. Don't try to write the whole gas mileage calculator in one line. Get the subtraction working first. Then add the division. One step at a time, same as everything else we've done so far.
Before Next Time
Write a tiny program that calculates something from your own life, gas mileage, grocery total split between roommates, whatever, using at least one of the math symbols from today. If it breaks, that's fine, figuring out why the computer hates you today is basically the job.