Concepts / What Is a Program?

What Is a Program?

A program is a set of instructions that a computer carries out.

  • Programming

From Instructions to Action

A program is a set of instructions that a computer carries out. When you write Python, you express those instructions in a form designed for people to read and write. The CPU, however, does not understand Python directly. It understands machine language, represented by zeros and ones. Running a program therefore requires a bridge between the language used by the programmer and the language understood by the CPU.

Python programhuman-readable instructionsTranslatorconverts the codeMachine languagezeros and onesCPUexecutes instructionsrunproducesis executed by
How does a high-level program become a sequence of actions performed by the computer?

Tracing a Python Run

Suppose you write a small Python program. The text is meaningful to you because Python uses a high-level language designed for human readability. The CPU's hardware has no direct understanding of that Python text. When you run the program, a translator reads the Python code and converts it into machine language for the particular CPU. That machine language is loaded into memory, and the CPU executes the resulting binary instructions.

Python codereadable statementTranslator readsPython instructionsMachine languageCPU-specific binaryinstructionsMemorytranslated code loadedCPU executioninstructions processedrunconvertsloadsexecutes
What happens between a high-level instruction and the CPU's executable instructions?
python
Worked example

Following x = 5 + 3

What changes as the CPU receives the operation expressed by x = 5 + 3?

  1. 1
    Human-readable instruction

    The Python statement is concise and readable to a programmer. It expresses an assignment and an addition.

  2. 2
    Translation

    A translator converts the statement into a longer sequence of machine-language instructions. The result specifies the low-level operations required by the CPU.

  3. 3
    CPU execution

    The translated instructions are loaded into memory and executed by the CPU.

Result

The same computation has been expressed first in Python and then in machine language. The Python statement is not itself a machine-language instruction; it is converted into instructions the CPU can execute.

Two Different Languages

High-level languageMachine language
Designed to be readable and writable by peopleUnderstood by the CPU
Can use readable syntax such as Python statementsIs represented by zeros and ones
Hides many CPU-specific detailsSpecifies detailed operations for a CPU architecture
Can be portable when a translator exists for the target machineIs tied directly to particular CPU hardware

High-level code and machine language express computation in fundamentally different ways.

Pythonreadable statementProgrammerwrites and readsMachine languagezeros and onesCPUexecutesusestranslated intounderstood by
What is the difference between code written for people and instructions represented as zeros and ones?

The difference is not merely visual. A Python statement is a human-friendly way to describe a computation. The corresponding machine language is a different expression of that computation, one that speaks directly to the CPU's architecture. For the example x = 5 + 3, the machine-language form is not simply the Python text written with different symbols. It is a longer sequence that specifies details such as which CPU registers to use, which memory locations to access, and which low-level operations to perform.

The Translator's Role

Definition

A translator is a tool that converts high-level language code into machine language that a CPU can execute. It solves the basic communication problem between human programmers and CPU hardware.

High-level codePythonTranslatorconversion toolMachine languagezeros and onesCPUhardwareinputconverts toexecutes
How does a translator connect a high-level instruction to CPU-executable instructions?

Portability Through Re-translation

Machine language is hardware-specific. Different processor types, including Intel, ARM, and AMD processors, have different machine languages. Machine language written for one processor type will not run on a different processor type. High-level code gains portability because it can be translated again for the CPU in another computer.

Python programsame source codeTranslatormachine AMachine language Afor CPU ATranslatormachine BMachine language Bfor CPU Btranslated ontranslated onproducesproduces
How can the same Python program run on computers with different CPU architectures?
Worked example

Sharing a Python Program

Why can the same Python program run on two computers with different CPUs?

  1. 1
    Write once

    The programmer writes the program in Python, a high-level language.

  2. 2
    Translate on the first computer

    A translator converts the Python code into machine language suited to the first computer's CPU.

  3. 3
    Translate on the second computer

    A translator on the other computer converts the same Python code into machine language suited to that computer's CPU.

Result

The machine-language outputs can be different, but the high-level Python program can run on both computers as long as each machine has a Python translator installed.

Binary Does Not Mean Simple

Machine language may appear simple because it uses only two symbols: zero and one. That appearance is misleading. Its syntax is more intricate than Python, and writing it directly is impractical for programmers. A machine-language sequence must express detailed CPU-specific operations. For a computation such as x = 5 + 3, the sequence can specify registers, memory locations, and low-level operations. The small alphabet does not remove complexity; it makes the instructions harder for people to read and write.

Zeros and onesbinary representationCPU registerswhich registers to useMemory locationswhich locations to accessLow-level operationswhat the CPU performsencodesencodesencodes
How can a simple-looking binary representation stand for many detailed operations?

Mistakes About Programs

  • Common mistake

    Assuming the CPU directly understands Python.

    What it looks like

    A learner writes a Python statement and imagines that the CPU executes that text exactly as written.

    Why it fails

    The CPU understands machine language, not high-level languages such as Python.

    Do this instead

    Remember the translation step: Python code is converted into machine language before the CPU executes it.

  • Common mistake

    Assuming machine language is easier because it uses only zeros and ones.

    What it looks like

    Binary appears simpler than Python because it has only two symbols.

    Why it fails

    Machine language has intricate syntax and must specify detailed CPU operations.

    Do this instead

    Separate the number of symbols from the difficulty of expressing instructions. Machine language is hardware-specific and impractical to write directly.

  • Common mistake

    Assuming the same machine-language program works on every CPU.

    What it looks like

    A machine-language program for an Intel processor is expected to run unchanged on an ARM processor.

    Why it fails

    Different processor types have different machine languages.

    Do this instead

    Use high-level code and translate it for the target machine.

  • Common mistake

    Thinking portability means the same machine code is used everywhere.

    What it looks like

    A Python program runs on two computers, so the learner concludes that both CPUs must be executing identical machine-language instructions.

    Why it fails

    Each computer's translator can produce machine language suited to its own CPU.

    Do this instead

    Portability comes from re-translation of the same high-level program on each target machine.

Best practice

When tracing a program, keep three layers separate: the high-level instructions written by the programmer, the translated machine language, and the CPU's execution of that machine language. This separation makes it easier to understand both why translators are necessary and why high-level programs can be portable.

Check Your Understanding

What do you think happens?

A Python program is sent from one computer to a friend's computer, and the two computers use different CPU types. What must happen before the friend's CPU can execute the program?

  • The friend's CPU executes the Python text directly.

  • The program is translated into machine language for the friend's CPU.

  • The original computer's machine language is copied unchanged.

  • The Python code is replaced with zeros and ones by the programmer.

Reveal answer
Answer:

The program is translated into machine language for the friend's CPU.

The source program remains high-level Python, but a translator on the friend's machine converts it into machine language suited to that machine's CPU.

EASY

In your own words, explain why a high-level language can be portable even though machine language is hardware-specific.

Hints
  • Identify which language is written by the programmer.
  • Identify which component converts that language.
  • Explain what changes when the target CPU changes.

Key Takeaways

  • A program is a set of instructions, but the CPU can directly understand only machine language represented by zeros and ones.
  • High-level languages such as Python are designed for human readability and are not directly understood by the CPU's hardware.
  • Translators convert high-level code into machine language that the CPU can execute.
  • Machine language is tied to particular CPU hardware, while high-level programs can be portable through re-translation on different machines.
  • Although machine language uses only two symbols, its syntax and CPU-specific details make it more complex to write than Python.

More concepts starting with W