Skip to main content
Text ready — video coming soon

Read Python: follow names, values and results

This is the moment you've been building up to: real code! In the last lesson, we saved the committee program in a file. Now let's open it and read it, line by line.

In this lesson, you'll learn to:

  • follow how names stand for values;
  • see how a calculation becomes output on the screen;
  • tell different kinds of values apart;
  • read an error message calmly, without guessing at a fix.

Why it matters: reading code is the skill you'll use to check what an AI writes for you. In this lesson you only read; you don't need to write anything.

Read the committee program line by line​

Python is a programming language. Here is our committee program, written in Python:

# Amounts recorded as received this month.
member_a = 1000
member_b = 1000
member_c = 500
total = member_a + member_b + member_c
print(total)

Let's go line by line.

Line 1 starts with #. That makes it a comment: a note for people reading the code. Python skips it.

Line 2, member_a = 1000, gives the name member_a the value 1000. Read it as "let member_a stand for 1000". A name that stands for a value is called a variable. Its value can be changed later.

Lines 3 and 4 do the same for the other two members.

Line 5 adds the three values. + means add. Python looks up what each name stands for, adds 1000 + 1000 + 500, and gives the answer the name total.

Line 6 uses print, which shows a value on the screen. The value to show goes inside the brackets. When the program runs, it shows:

2500

Giving a name a value and showing a value are two different instructions. Line 5 works out the total. Only line 6 shows it.

A program can work and show nothing​

Here is a whole program with just one line:

total = 2500

It runs without any problem, but nothing appears on the screen. It gives total a value and stops. There is no print, so nothing is shown.

What kind of value is it?​

Every value has a type: the kind of value it is. The type decides what you can do with it.

ExampleTypeWhat it is
1000IntegerA whole number
12.5FloatA number with a decimal point
"Recorded"StringText, written inside quotation marks
True or FalseBooleanOne of two values: true or false

Quotation marks matter. In this program, message is a name, and "Recorded" is the text it stands for:

message = "Recorded"
print(message)

It shows:

Recorded

Python showed the text that message stands for, without the quotation marks. If you wrote print("message") instead, with quotation marks, Python would show the word message itself. Without quotation marks, it's a name. With them, it's text.

Read an error as a clue​

An error is not a disaster. It tells you which step failed. Here is a program with a mistake in it:

member_a = 1000
print(member_c)

This program never gave member_c a value. Python stops, and the last line of its message says:

NameError: name 'member_c' is not defined. Did you mean: 'member_a'?

A NameError means the program used a name that has no value yet. Here, Python also suggests a name that looks similar. But be careful: if you just follow the suggestion, the program will show Member A's amount instead of Member C's. The suggestion makes the error go away. It doesn't know which amount you wanted. Only you know that.

Here is a different kind of mistake:

print("Received"

The opening bracket is never closed. Python stops, and the last line of its message says:

SyntaxError: '(' was never closed

Syntax means the rules for writing code correctly, like the grammar of a language. A syntax error stops the program before any of it runs.

Fixing an error only makes a program run. It doesn't make it right. A program can run without errors and still answer the wrong question, like the 3,000 total. Always keep in mind the result you actually want.

Check what you can now explain​

Without looking at the results above:

  1. In the committee program, which values reach the addition on line 5?
  2. If Member C's amount changes from 500 to 750, what should the program show?
  3. Why does total = 2500 show nothing?
  4. What is missing in the last broken example?

You've just read a real program and predicted what it does. In the next lesson, we'll see what happens when someone presses a button in a web app.