Python's Not (Just) For Unicorns

An interactive introduction to programming in Python, for human beings and whoever else

Chapter 6

Data types and error messages

Remember how we can add numbers together? Let’s try it real quick.

2 + 2

What do you think happens if you try to add words together? Let’s try it: how about adding together hello and world? Remember to use quotes for each of them!

  • Hint: You should quote them separately, don’t write "hello world"
  • Hint: Try to add "hello" and "world" together
  • Hint: Type "hello" + "world" and press enter.

Hm, weird, they’re kind of smushed together. Makes sense if you think about it, I guess, but a space would be nice. Maybe try to add a space in the middle of them, too?

  • Hint: You can use a space like " ". It looks like nothing is inside, but I promise it’s a space!
  • Hint: Add the three pieces together - hello, the space, and world.
  • Hint: Type "hello" + " " + "world" and press enter
  • Hint: You could also do "hello " + "world", but that’s not as fun!

Okay, we’re genius experts now! We’ve done math with numbers, math with words, how about math with… numbers and words?!

Try to add together 2 and "hello". (Hint: it ain’t gonna work)

Hooray, an error message! Just what we wanted.

We’ve already seen a couple error messages, but we didn’t read them very closely. We didn’t care! But this time let’s look at each part of the error to see what it might mean.

TypeError: unsupported operand type(s) for +: 'int' and 'str'

  • TypeError: probably means it’s… an error… about… types? This part will become helpful after you’ve been programming for a while, but it’s super mysterious now.
  • unsupported operand type(s) for +: I don’t know what an “operand type” is, but the ending makes it seem like it’s an error about addition. That makes sense, since we were trying to add something.
  • 'int' and 'str': Hm, what are we trying to add? It sounds like Python thinks we’re adding int and str.

If you paid attention in math class, you’ll know that whole numbers - 0, 1, 2, 3, etc - are called integers. That’s what int means - it’s talking about the 2! According to Python, 2 is an integer.

And str? It stands for string. Strings are what Python calls text, or words, or sentences. Every time we did "hello" or "I hate bread" or any of that, the quotation marks turned our text into a string that Python could understand. According to Python, "hello" is a string.

“Integer” and “string” are called data types. They’re… types of data. You’ll learn a few more over the next several chapters.

Now that we know about data types like integer and string, if we go back to the error message, it says unsupported operand types for +: 'int' and 'str'. It sounds like it might just mean, “you can’t add together integers and strings!”

But let’s say I can’t give up. I really want to combine the 2 with “hello”. How am I going to do it? How can we solve this problem?

Well, same way we learn to fix a doorknob or how to make spaghetti: we’re going to ask a search engine.

Let’s actually try this like Real Live Programers: go search around online and see if you can figure out how to solve our problem of adding 2 and "hello". Use the console below to try out answers.

Try to google something on your own, but you can also find suggestions in the hints below. If you can’t get it, no big deal, but at least give it a try!

  • Hint: The actual error message, TypeError: unsupported operand type(s) for +: 'int' and 'str'
  • Hint: What you’d like to do, in Python-y words python add integer and string
  • Hint: What you’d like to do, in normal words python combine number word

Our mission: Try to combine 2 and hello

Okay, you’re back. I’m back. We’re both back. It really isn’t important if you solved the problem (we’ll talk about answers later), what I’m interested in is what did you search for to try to solve the problem? If you haven’t yet, check the hints above to see some suggestions I thought of - there aren’t any wrong answers, but there are definitely several different approaches.

Whatever you searched for, you probably ended up reading answers on a site called StackOverflow, which is where everyone goes to ask questions about programming, kindly answer questions about programming, meanly answer questions about programming, fight about programming, etc. Just normal life stuff.

You’ll be on StackOverflow a lot. A lot lot. It’s cheating if you use it in a Computer Science class, but once you’re in real life you’ll probably use it about ten thousand times a day.

Yes, there are a lot of answers to every programming question out there, but it’s usually pretty tough to actually use them. Learning to use other peoples’ code and dissect fights about the “right way” to solve a problem is a whole other skill to learn. We’ll talk about it later!

For now, just know that error messages are your friends. And just like it’s helpful to understand your friends, it’s helpful to understand your error messages. It makes life easier. Read your errors, spend time with your errors, understand your errors.

But back to the problem: it turns out you can turn an integer into a string by doing, for example, str(9). Then you combine that with the string. According to that link, it looks like this:

"asd" + str(9)

Seems right, ’eh?

You could also have put quotation marks around the 9, turning it from the integer 9 into the string "9". Quotes turn it into a string, just like str turns it into a string.

"asd" + "9"

Now it’s your turn: try to combine 2 and hello again, this time using that new trick! Remember your quotes, and if you get an error just think hard and try again (or search for a better explanation than mine).

Chapter summary

We learned about data types like integers and strings, and learned how to search for solutions when we encounter error messages. It sounds like we’re going to be reading StackOverflow a lot!