Skip to content
Utah Community Learning

What a variable is, in plain language

About 15 minutes

What a Variable Is, in Plain Language

Okay so here's the thing. You've been typing print("something") and watching words show up. That's great, but it's also kind of a party trick. Today we're doing something a little more useful. We're going to store stuff.

That's it. That's the whole concept. A variable is a box you put stuff in, and you write a name on the box so you can find it again later.

Why you'd even want this

Say you're writing a program and you need to use somebody's name three different times. You could type "Jared" three separate times. But what if it's not Jared, what if it's a different name every time someone runs the program? You need somewhere to put that name so you can grab it back out whenever you need it.

That's a variable. In Python it looks like this:

``python name = "Jared" print(name) ``

Run that and it prints Jared. The box is named name, the stuff inside the box is the text "Jared", and the equals sign is basically Python's way of saying "put this on the right into the box on the left."

I know the equals sign is doing something a little different than it did in math class. In math class, = means "these two things are the same." In Python, = means "take whatever's on the right and shove it into the box on the left." Different job, same symbol. Just one of those things you accept and move on.

Boxes can hold different kinds of stuff

``python age = 21 temperature = 98.6 city = "American Fork" is_raining = False ``

Text goes in quotes. Numbers don't. True and False are their own special thing called a boolean, capital letter and all, no quotes. Python's picky about that stuff and it'll tell you so, loudly, in red text. That's normal. You'll get used to reading those messages instead of fearing them.

Boxes can change what's inside them

This is the part that trips people up because it feels weird the first time.

``python score = 0 print(score) score = 10 print(score) ``

That prints 0 then 10. The box didn't get renamed, the contents changed. That's literally the point of a variable. If it never changed, you'd just type the number directly and skip the whole thing.

Here's where my son comes in. Jared's 21, thinks he's forgotten more about computers than I'll ever know, and once watched me debugging something by sprinkling print() statements all through my code to see what was in which box at which moment. He looked over my shoulder and said, "oh, you're using print statements like a caveman." He was not wrong. It IS the caveman way. It is also fast, it works, and I still do it constantly. When you're not sure what's inside a variable at some point in your program, just print it. Don't be embarrassed about it. The fancy tools exist, sure, but a well-placed print(score) tells you what you need to know in about four seconds.

Naming your boxes

You get to pick the name. Pick something that describes what's in it. age is good. x is technically legal but tells you nothing six months from now, or honestly six minutes from now.

A few real rules, not preferences: - Names can't start with a number. 2cats is not allowed, cats2 is fine. - No spaces. Use an underscore, like favorite_song. - And here's a super important one I learned the loud, dumb way. Don't name a variable something Python already uses for its own stuff. Early on I named a variable list because, well, it was a list of things. Seemed obvious. It broke everything downstream and I could not figure out why for a solid hour, because Python already uses the word list to mean something specific, and I'd just stomped all over it. So we'll avoid words like list, str, print, type. If your editor turns the word a different color when you type it, that's a hint it's already spoken for.

Try this at home

Open your editor and type this yourself. Don't copy and paste, actually type it, your fingers remember things your eyes skim right past.

```python first_name = "your name here" favorite_food = "pick one" number_of_pets = 0

print(first_name) print(favorite_food) print(number_of_pets) ```

Change the values, rerun it, watch what prints. Then try changing number_of_pets partway through the file and printing it twice, once before and once after, so you can see the box's contents actually swap out.

If you get an error, good. That means you're actually doing it and not just reading along. Read the error message slowly, it's usually telling you exactly what it didn't like, in its own weird robot way.

Before next time: try naming a variable str or print on purpose, just to see Python get mad about it. Better to break it on purpose now than get confused by it later.

What a variable is, in plain language · Introduction to Coding with Python · Utah Community Learning