Topic 01

What a Program Really Is

Concept

You have used programs every day for years: a browser, a music player, the app that tells you when the bus arrives. Each of them began as a person writing instructions. A program is exactly that: a list of instructions that a computer follows, one after another, exactly as written.

The key word is exactly. A computer has no common sense and never guesses what you meant. Picture a recipe handed to someone who has never cooked and takes every word literally. "Add salt" with no amount leaves them stuck. A computer in that position does not ask. It stops, and prints an error saying what it could not do. That sounds harsh, but it is the reason programming can be learned at all: the rules are fixed, and the machine applies them the same way every time.

The gap between meaning and instruction
What you meant
Add some salt, then give it a stir.
What a computer needs
Add 5 grams of salt. Stir for 10 seconds. Then stop.

Instructions, Followed Literally

A computer does what the text says, not what you intended. When the two differ, the result is called a bug: the program runs, but it does the wrong thing, or it stops with an error. Every programmer, at every level, spends a large part of the day in that gap between intention and instruction. The skill you are about to learn is not avoiding the gap. It is closing it quickly.

This is also why a single wrong character can stop a program. A missing bracket is not a small typo to the computer. It is an instruction that no longer makes sense, and the computer has no way to decide what you probably meant.

Why We Need a Language

Deep down, the processor inside your computer understands only numbers. Nobody writes programs in raw numbers any more. Instead we write in a programming language: a precise written form that a person can read, with strict rules about spelling and punctuation. A separate program then turns that text into action. For Python, that program is called the interpreter. It reads your instructions and carries them out, one by one.

How text becomes a running program, and what happens inside the machine while it runs, is a story of its own. Computing Foundations from Zero tells it in its fourth chapter. This book takes the next step and has you write the text.

What Python Looks Like

Here are three lines of Python. Do not type them yet; just read them. The first line gives the name price to the number 12. The second asks whether the price is less than 15. The third, which only runs if the answer is yes, prints a short message. Read aloud, it is almost an English sentence: if the price is under fifteen, say it is a bargain.

A first look at Python, for reading only
price = 12
if price < 15:
    print("That is a bargain.")

Every line in that example is something this book will teach properly: giving a value a name in Chapter 2, making decisions in Chapter 3, and printing text as early as this chapter's last topic.

What This Book Will Do

By the last chapter you will write and maintain a script of a couple of hundred lines. It will read a file, keep its data between runs, fetch information from the internet, cope with bad input without crashing, and come with tests that prove it works. Along the way you will learn to read other people's Python, which is at least as important as writing your own.

The method is the loop in this chapter's figure: write some instructions, run them, read what happens, and change them. Nothing more mysterious than that. The rest is practice, and learning to read the messages the computer sends back.

Common Confusions
  • "Programmers memorise the whole language." They look things up all day long. What they carry in their heads is the habit of thinking in precise steps and the ability to read an error message calmly.
  • "You need to be good at maths." This book needs the arithmetic you already use for shopping and bills. Programming is much closer to writing very precise instructions than to algebra.
  • "The computer understands what I mean." It understands only what you wrote. That is exactly why the error messages matter, and why this book reads them aloud with you from the start.
Why It Matters
  • Knowing the machine is literal turns frustration into a method. When something goes wrong, the question is always the same: what exactly did I write?
  • Every other technical course in this catalogue talks about programs. After this book, you can write one.

Knowledge Check

Why can a single missing character stop a program completely?

  • The computer tries to repair the line and gives up when it cannot
  • The computer follows the text literally and cannot guess the intent
  • A missing character damages the file on the computer's hard disk
  • The program stops itself on purpose to protect the other programs

What does Python's interpreter do?

  • It translates English sentences into Python code for you to use
  • It checks ordinary documents for spelling and grammar mistakes
  • It reads your Python instructions and carries them out in order
  • It chooses the best instructions to run from a library of programs

What is a bug?

  • A virus that gets into a program from the internet and damages it
  • A difference between what you meant and what you actually wrote
  • A computer that runs too slowly for the program it has been given
  • A feature that most people who use the program happen to dislike

Which skill does this page say matters most when something goes wrong?

  • Remembering every rule of the language without looking anything up
  • Deleting the program and starting again from an empty file each time
  • Being able to do advanced mathematics quickly in your head
  • Asking what exactly you wrote, and reading the error message calmly

You got correct