Basic

A value is something the machine holds

Before any rule or loop, there is something simpler underneath: a value. A value is a fact the computer is holding, a number or a piece of text. Your first program does nothing but hand one value to the screen.

What you will see

4

About 2 min of reading

Try It

Run the smallest program and watch a value reach the screen.

1

Print a number you wrote down. Nothing is computed here, the value is already 4.

print(4)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
4
2

Print a sum instead. The computer works it out before printing.

print(2 + 2)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
4
3

Put the two on separate lines. One run now shows two lines, both 4.

print(4) print(2 + 2)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
4 4
4

Now print the quoted form. It looks identical but it is text, not a number.

print("4")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
4
The screen shows 4 for both the number and the text. print cannot show you the difference, so types only start to matter the moment you compute with them.

Data Flow

You write something that makes a value, the computer works it out, and print shows the result on the screen.
1 You write an expression
↓
2 The computer works it out
↓
3 print shows the value
↓
4 The screen shows 4
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

print(4) 4
Why this works
Why values come first

A program is a machine that follows instructions, and every instruction you will ever write moves or combines values. If you can name the values in a program, you can follow what it does. Reading a program is mostly a search for its values and the names attached to them.

What print does

print does not create a value, it shows one that already exists. The value 4 was made by 2 + 2 before print ever ran. That separation matters later, when a value is stored instead of shown, and when showing a value is only for your benefit while the real work happens elsewhere.

Examples

The smallest program

print(2 + 2)

There is no setup here. The computer works out 2 + 2, which is the value 4, and print hands that value to the screen. Everything a program does starts from values like this one.

A value on its own

print(4)

This prints 4 too. The difference is that 2 + 2 had to be worked out and 4 was already written down. Both end as the same value, which is why the screen cannot tell them apart.

Values are not sentences

print("4")

This prints 4 as well, but the quotes make it text, not a number. It looks the same and behaves differently the moment you try to add to it. Keep that in mind from the start.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
A name is a handle for a value
A name is a handle for a value →