Python's Not (Just) For Unicorns

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

Chapter 18

Updating variables

Oof, wait a second, let’s talk about something. Let’s talk about updating variables.

Variables are called variables because they can vary, but sometimes it takes a little thought to understand how that can work. For example, in the code below we start with num being set to 0, but then we make it bigger, and bigger, and bigger.

num = 0
print("The number is", num)

num = num + 1
print("The number is now", num)

num = num + 1
print("The number is up to", num)

num = num + 20
print("The number ends at", num)

How does that look? We re-use the num variable again and again, changing it each time. One way of thinking about it:

  • We set num = 0 to, yes, make num be 0.
  • The first time we run num = num + 1
    • It calculates the right-hand side, num + 1
    • This uses the current value of num (0), making 0 + 1. That’s 1.
    • It then saves that 1 back into num, throwing away the old value for num
    • Now num is 1!
  • The second time we run num = num + 1
    • It calculates the right-hand side, num + 1
    • This uses the new value of num (1), making 1 + 1. That’s 2.
    • It then saves that 2 back into num, throwing away the old value for num
    • Now num is 2!
  • Then we run num = num + 20
    • It calculates the right-hand side, num + 20
    • Since num is 2 now, that’s 2 + 20. That’s 22.
    • It then saves that 22 back into num, throwing away the old value for num
    • Now num is 22!

It might feel weird to see the same variable on both sides of the equals sign, but I promise you’ll get used to it! Just know that you’re overwriting the old num every time you do num = ... - the old num disappears into dust, and you’ll never be able to get it back! (Not that you’d want to)

Two common reasons to update a variable are counting things, or if you’re too lazy to have separate variable names. Try the following quiz!

points = 0

# Ask about New York, answer is 'Albany'
answer = input("What's the capital of New York?")
if answer == "Albany":
  points = points + 1

# Ask whether it's boring, answer is 'yes'
answer = input("Is this boring?")
if answer == "yes":
  points = points + 1

print("You got", points, "answers right")

Every time I ask a question I check the answer - if they got it right, I make points one larger. Making something one larger is called incrementing1. When you have a variable you’re using for counting you’ll often increment it for one reason or another. I increment points once for each correct answer.

Also, notice that I used the variable name answer twice. The second time, when I ask Is this boring?, I save the response into answer and the computer totally forgets the previous answer. Since I won’t need the first response ever again, that’s okay!

Actually, you know what? I want you to make a quiz like that. And I want it to be about these questions:

questions answer
What is the capital of Missouri? Jefferson City
Who is the “father of Medicine?” Hippocrates
What is the square root of 64? 8

And these might be a little trickier than you think!

points = 0

print("You got", points, "points")
  • Hint: You can use the if/points code from up above as a template. Cutting and pasting similar, working code is going to be your best friend when programming!
  • Hint: Make sure you put : at the end of all of your if statements.
  • Hint: Putting quotes in quotes can make Python confused about where the string actually ends. You can either use the opposite quotes for the outer quotes - like 'My name is "Charles"' - or “escape” them by using backslashes "My name is \"Charles\"". Opposite quotes is probably easiest, and we’ll talk more about escaping in the future.
  • Hint: Remember how the result of input is a string? Asking if answer == 4 isn’t going to work because one is a string and one is an integer!

[1] Incrementing doesn’t have to be one larger, it can be any amount! But it’s normally one larger.

Chapter summary

We talked about how to update variables based on themselves, learned that increasing the value of a variable is called incrementing, and gained the knowledge that Missouri’s capital is Jefferson City. That last one is an especially important life lesson!