An interactive introduction to programming in Python, for human beings and whoever else
Chapter 18
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:
num = 0
to, yes, make num
be 0
.num = num + 1
…
num + 1
num
(0
), making 0 + 1
. That’s 1
.1
back into num
, throwing away the old value for num
num
is 1
!num = num + 1
…
num + 1
num
(1
), making 1 + 1
. That’s 2
.2
back into num
, throwing away the old value for num
num
is 2
!num = num + 20
…
num + 20
num
is 2
now, that’s 2 + 20
. That’s 22
.22
back into num
, throwing away the old value for num
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")
if
/points
code from up above as a template. Cutting and pasting similar, working code is going to be your best friend when programming!:
at the end of all of your if
statements.'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.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.
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!