Naming Things So Future-You Can Read Them
Okay so here's the thing. You already know how to make a variable. You know numbers and words are different. Now we've got to talk about the part nobody warns you about, which is what you actually call the thing.
This sounds like the boring lesson. It's not. Bad names are where half of beginner code goes to die.
Why this matters more than it seems like it should
When you write x = 24, that works fine right now, while you're staring at four lines of code. But you're not going to stay at four lines. You're going to have forty, then four hundred, and future-you is going to open that file after not looking at it for two weeks and have absolutely no idea what x was supposed to be. Was it an age? A photo count? The temperature? No clue. Future-you is annoyed at present-you, and present-you isn't even here to defend yourself.
So we name things so future-you, tired and half paying attention, can read the code like a sentence and just get it.
That's genuinely the whole goal. Not clever. Not short. Readable.
The naming rules that actually matter
Python's pretty relaxed about what you can call a variable, but there are a few real rules:
- Names can have letters, numbers, and underscores.
- They can't start with a number.
2coolis not allowed, Python will yell at you. - No spaces. Use an underscore instead, like
photo_count, notphoto count. - Names are case-sensitive, so
Ageandageare two totally different variables to Python. This trips people up constantly.
Beyond the actual rules, here's the practical guidance. Name the variable after what it holds, not after where it came from or how you feel about it. total_cost is good. temp is vague. thing2 tells future-you nothing.
Okay so here's the thing that'll bite you
There are words Python already uses for its own stuff, and if you name a variable one of those words, things break in confusing ways.
I learned this one the loud way, so let me save you the hour I lost. Early on I named a variable list, because, well, it was a list. Seemed reasonable. Ran the program and everything downstream just started breaking, and I could not figure out why. I stared at it for way too long before I realized Python already has a built-in thing called list, and I'd just told Python to forget what that word means and use my grocery items instead. Total mess. Took me an hour to untangle it because the error messages weren't exactly screaming "you renamed a core Python word" at me.
So: avoid naming variables things like list, str, print, type, input. If you're not sure whether a word is already taken, you can always just try it and see if it turns a weird color in your editor, most editors will highlight built-in words differently. Or just get in the habit of being a little more specific than the bare minimum. grocery_list instead of list. Costs you two extra letters, saves you an hour of confusion.
My actual opinion on this
Clever code is bad code. I mean that. I once wrote this beautifully compact one-liner, felt like a genius, showed it to my son. Came back to that same code a week later and could not read my own work. Had to sit there and reconstruct what past-me had been thinking. Nobody gave me a prize for being clever. I just made my own life harder.
Boring and clear beats clever every single time. If you're tempted to shorten a name to save keystrokes, don't. Typing number_of_photos instead of n costs you nothing and saves future-you a headache.
Try this at home
Open your editor and make a few variables, but be deliberate about the names this time:
`` first_name = "Jordan" age = 34 favorite_snack = "cheese, obviously" print(first_name) print(age) print(favorite_snack) ``
Now go back and rename them badly on purpose. Call them x, y, z. Run it, it'll still work fine, computers don't care about your feelings. Then close the file, go make coffee, come back in ten minutes and try to figure out what x was without scrolling up. That little experiment is basically what "future-you" feels like, except future-you doesn't get ten minutes, future-you gets two weeks and a full memory wipe.
A quick word on capitalization style, since people ask: most Python folks write variable names in what's called snake_case, all lowercase with underscores between words, like total_photo_count. You don't have to follow this. Python won't enforce it. But it's the common style, and matching the common style means other humans, and future-you, can read your code faster because it looks like what they're used to seeing.
Before next time
Go back through any code you've written so far in this class and rename at least one lazy variable, something like x or data, into something a stranger could understand. See how much easier it reads.