Skip to content
Utah Community Learning

Numbers vs. words (and why the computer cares)

About 20 minutes

Numbers vs. Words (and Why the Computer Cares)

Okay so here's the thing. Last lesson you made your first variable, put some stuff in a box, gave the box a name. Super good. Today we talk about what kind of stuff you can put in the box, because Python actually cares, and it will tell you so in a very unfriendly way if you get it wrong.

Here's the setup. Type this into your editor and run it.

``python age = 43 print(age) ``

That works fine. 43 there is a number. Python calls this an integer, or int for short, which just means a whole number with no decimal point.

Now try this instead.

``python age = "43" print(age) ``

Looks almost identical, right? But those quote marks change everything. Now 43 isn't a number to Python anymore. It's just text. Python calls text a string, because it's a string of characters lined up in a row. Could be "43", could be "hello", could be "my cat's name is Gary". Doesn't matter what's inside the quotes, if it's in quotes, it's a string.

So why does this matter? Because numbers and strings behave completely differently when you try to do stuff with them. Watch this.

``python x = 5 y = 3 print(x + y) ``

That prints 8. Totally normal, that's addition, math is happening.

Now watch this one.

``python x = "5" y = "3" print(x + y) ``

That prints 53. Not eight. Fifty-three. Python isn't adding those two things, it's just smooshing the text together, like taping two pieces of paper end to end. That's called concatenation, which is a fancy word for "sticking things together." You don't need to remember that word. I forget it half the time myself and just call it "string smooshing" in my head.

And here's the one that actually breaks people's programs. Try this:

``python age = "43" print(age + 1) ``

Run that and Python throws an error at you. Something like can't concatenate str and int, which is Python's way of saying "buddy, one of these is words and one of these is a number and I don't know what you want me to do with that." It's not being difficult on purpose. It genuinely doesn't know if you want it to do math or just stick things together, so it refuses to guess.

This trips up basically everyone starting out, so if it trips you up too, you're right on schedule.

Quick rule of thumb

If it's something you'd do math with, a price, an age, a count of something, it should probably be a number, no quotes. If it's something you'd read out loud as words, a name, an address, a color, it should be a string, in quotes.

Type these into your editor, one at a time, and run each one. Don't copy and paste them. I mean it, type them yourself. Your fingers pick up on stuff your eyes skim right past, and typing it out is half of how this sticks.

``python first_name = "Camille" zip_code = "84003" temperature = 68 kids_in_class = 12 ``

Quick note on that zip_code one. A zip code looks like a number but it's a string on purpose, because you'd never do math with a zip code, and some zip codes start with a zero that a real number would just chop off. Little gotcha, worth knowing early.

A story about this exact moment

I had a woman in one of my very first classes, total beginner, never touched code before in her life. We got to this exact spot, numbers versus words, and she typed out a tiny little program, four lines, nothing fancy, just a variable holding her name as a string and one holding her age as a number, printed both back out.

She ran it. It worked. And she gasped. Out loud. In a room full of adults staring at laptops. Loudest reaction I've ever gotten from four lines of text on a screen.

That's the whole reason I keep teaching this class. Not the big impressive stuff later on, this part right here. The moment it clicks that you told the computer what kind of thing something is, and it listened.

One opinion, since we're here

People assume coding is a math class in disguise. It mostly isn't. What you just did up there, deciding "is this a number or is this words," that's not math, that's just paying attention. Most of coding is stubbornness and reading the error message carefully instead of panicking at it. The actual math shows up way less than everybody expects. I promise.

Before next time

Try writing three variables of your own, one number, one string, and one that you're not sure about, and see if you can guess how Python will treat it before you run it. If you get an error message, don't panic, just read what it says. It's usually telling you exactly what's wrong, it's just talking like a computer about it.

Numbers vs. words (and why the computer cares) · Introduction to Coding with Python · Utah Community Learning