SkillGridLearn computer science by doing

Values and types: why 1 is not "1"

A type is a set of allowed values. That one sentence from logic:1 explains every strange error a beginner meets.

13 min · 40 XP

A program is a pile of values that get moved around. A number, a piece of text, a yes or no, a shopping list of scores. Everything else in this lesson is about one question: what kind of thing is this value, and what is allowed to happen to it.

In logic:1 a type was just a set. The boolean set has exactly two members: true and false. The integer set holds whole numbers. The string set holds text of any length, including the empty string. Saying that a value has a type means saying which set it came from. Every operation you write is a claim about sets: addition wants two numbers, joining text wants two strings.

bool   = { true, false }
int    = { ..., -2, -1, 0, 1, 2, ... }
string = any sequence of characters

age + 1   needs a value from int
"1" + 1   mixes a value from string with a value from intA type is a set of values, and an operation is a claim about sets.

Here is the same handful of values written in three languages. Do not try to memorise this. Read it the way you would read the same sentence in three accents: the words change, the meaning does not.

name = "Ada"
age = 36
tall = True
scores = [90, 85, 77]
prices = {"soup": 2.50, "juice": 1.90}Python: no type written by hand, the value decides.
const name = "Ada";
const age = 36;
const tall = true;
const scores = [90, 85, 77];
const prices = { soup: 2.50, juice: 1.90 };JavaScript: const for a name that will not be reassigned.
var name string = "Ada"
var age int = 36
var tall bool = true
scores := []int{90, 85, 77}
prices := map[string]float64{"soup": 2.50, "juice": 1.90}Go: the type is written next to the name, and the compiler checks it.

Now the classic beginner disaster. A signup form asks for your age. The form sends text, because everything a form sends is text. Your code then tries to add one to it.

form sends: age = "16"
8 + 1  = 9
"16" + 1 = ?Eight plus one is nine. What is the text "16" plus one?

The last idea is mutability, and it costs people more debugging hours than anything else in this lesson. A list is one object that many names can point at. Change the list through one name and every other name sees the change, because there was only ever one list.

scores = [90, 85]
same = scores
same.append(77)
print(scores)

[90, 85, 77]Two names, one list. The change is visible through both.
scoressame[90, 85]namesnamesone list, two namesappend 77[90, 85, 77][90, 85, 77]

Collections also carry the Big-O idea from logic:1. Choosing the right collection is often worth more than rewriting your loops. A list is for order and for walking through everything. A map is for looking things up by key. The cost table below is the whole justification for maps existing.

list.append(x)      O(1)
map[key]            O(1) average
x in list           O(n)
x in map            O(1) averageThe cost of an operation is the cost of the collection that holds the values.

Practice

  1. A signup form sends the age as text: age = "16". Your code runs age + 1. What is the honest description of what went wrong when this fails?

    Hints
    • The field arrives from a form, and a form only ever sends text.
    • Ask which of the two values lives in the string set and which one lives in the number set.
  2. Match each idea with the collection that is built for it. The point is the idea, not the syntax.

    Hints
    • Python and Go spell the same kind of collection differently: a list of names is still a list of names.
    • A map answers what the value for a given key is without walking the whole collection first.
  3. Put the steps of reading an age from a form into the right order. This is the shape of nearly every program that talks to the outside world.

    1. 1Check that the text really looks like a whole number
    2. 2Convert the text into a number
    3. 3Send the result back out as text again
    4. 4Read the raw text exactly as the form sent it
    5. 5Do the arithmetic on the number
    Hints
    • Nothing can be numbered until it is a number.
    • The check comes before the arithmetic: a program never does step three if step two could not run.