Python's Not (Just) For Unicorns

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

Chapter 4

Python basics

We live in the future, but a computer still can’t walk my dog. It’s a little disappointing, but computers only know how to do one thing: math! Math this, math that, math everything. It’s all their little robot brains were built to do!

If we can somehow figure out how to use math to walk a dog, we’d be all set1. But for now it’s me and you and Dog and waking up at the crack of dawn every day.

Since Python is good at math, we might as well start with that. We can kick the tires by asking her the age-old question of what 2 plus 2 is.

2+2

Amazing! Incredible! Unbelievable!

That’s right, it’s four, and Python knew it immediately. From what I can tell, she’s an A+ genius, and we couldn’t have done it without her.

And you know what? I think you have it in you, too. In fact, I know it. Try the same thing we just did, typing in the box below, to add together 2 plus 2.

  • Hint: Click the box below, type 2+2, and hit enter

Let’s push the boundaries, let’s go further, what happens if we use even larger numbers?

170+204

Unbelievable! And what if we put some spaces in there instead of squashing everything together?

170 + 204

Works like a charm! This is honestly a dream come true.

Maybe we can get really crazy now? Try something really big, like 9999 + 9999.


  • Hint: Click the box above, type 9999 + 9999, and hit enter

But hold on for a second, let’s back up, let’s talk about what we’re seeing. I understand the math, but what is the box we’re typing it in? What’s that blue arrow-ish thing? What’s any of this?

The weird >>> thing isn’t actually Python code. It’s called a prompt, and it just means “hey, this is where you’re going to type Python.” So when I ask you to type your own Python, that’s where you’re going to do it.

For example, the box below really wants you to multiply 300 and 25. See the prompt? It means you can type Python code after that. Try to multiply together 300 and 25.


  • Hint: You can multiply by using the * symbol
  • Hint: Type 300 * 25 next to the prompt and hit enter

After you type something next to the prompt and hit enter, Python runs the code immediately and displays the result. 7500, in this case! Then it displays another prompt, which means you’re free to type even more Python and hit enter again, if you’d really like to.

That whole box is called the console. It’s just a place where you can type and run code, one line at a time. You can also write whole Python programs and run multiple lines of code, but we’ll save that for later!

Since we understand multiplication now, how about division? What about three divided by nine? Or nine divided by three? Try them both in the console below — you can divide using /.


Some of the consoles have prompts in them — you can type in those! — and some of them are just for examples. For example, the console below doesn’t have a prompt, it’s just to show us how parentheses work when doing math.

5 * (10 - 5)

Pretty convenient, right? We’re going to be using the console for the next few lessons, so you’ll want to get comfortable with using it.

In case you were on fire with curiosity, here are some math things you can do in Python:

Math word Symbol Example Answer
Addition + 5 + 2 7
Subtraction - 5 - 2 3
Multiplication * 5 * 2 10
Division / 5 / 2 2.5
Exponent ** 5 ** 2 25
Modulo (Remainder) % 5 % 2 1

So now, a question: how many seconds are in a day? I’m too lazy to google it, so let’s just ask Python. Have her multiply it out:

  • 60 seconds in a minute, times…
  • 60 minutes in an hour, times…
  • 24 hours in a day.

  • Hint: You can multiply more than one number at a time by typing something like 2 * 3 * 4
  • Hint: Try to multiply the hours in a day by the minutes in an hour by the seconds in a minute
  • Hint: Type 60 * 60 * 24 and hit enter

Congratulations! That math was the very first boring thing you’ll have to do. There are many, many more, but you’re definitely making progress.

Chapter summary

We learned to use the console to run Python code, one line at a time. We type our code next to the prompt, hit enter, and ta-da! Magic happens, and the answer is printed out. So far we’ve done simple math problems like addition and multiplication.

[1] Self-driving cars actually work by seeing and steering and driving with math, so a robot to walk a dog isn’t totally impossible!