Programming and Python
Learn how a task becomes an algorithm and then a program, and where Python fits. Build a clear model of input, processing, output, source code, and the runtime that executes it.
A computer can carry out instructions quickly and consistently, but the instructions must state the task in a form the computer can follow. Programming is the work of creating, testing, and improving those instructions.
From a Task to a Program
Suppose we have four temperature readings:
18, 21, 20, 23
We want their average. A person familiar with averages may see the calculation immediately. A computer needs the process stated explicitly:
- receive the four readings
- add them
- count how many readings there are
- divide the total by the count
- report the result
For these readings:
total = 18 + 21 + 20 + 23 = 82
count = 4
average = 82 / 4 = 20.5
The goal, “find the average,” is a task. The ordered method for completing it is an algorithm. A program expresses that method in instructions a computer can run.
Input, Processing, and Output
Many small programs can be understood through three roles:
| Role | Question | Temperature example |
|---|---|---|
| Input | What information enters the program? | Four temperature readings |
| Processing | What does the program do with it? | Add, count, and divide |
| Output | What result becomes available? | The average, 20.5 |
Not every program has such a simple shape, but this model is useful when a program is new or confusing. It separates the information the program receives from the work it performs and the result it produces.
What Programming Involves
Writing code is one part of programming. The larger process is:
- decide what the program should accomplish
- identify the inputs and desired outputs
- break the work into smaller steps
- express those steps as code
- run the code and inspect what happens
- correct the code or the original plan when the result is wrong
The first plan is not always correct. The first code is not always correct either. Testing and revision are ordinary parts of programming, not signs that the process has failed.
What a Programming Language Does
Computer processors work with low-level machine instructions. Writing useful software directly in those instructions would make most tasks unnecessarily difficult.
A programming language gives people a more readable way to describe data and operations. Each language has vocabulary, punctuation, and rules for arranging code. These rules are called its syntax.
Source code is the text a programmer writes. Another program translates or executes that source code so the computer can perform the requested work.
Where Python Fits
Python is a general-purpose programming language. Its syntax is relatively compact, and the same language can be used for small calculations, data analysis, web services, scientific computing, and machine learning.
A file containing Python source code is not enough by itself. A Python runtime must read and execute it. In this book, “Python” may refer to the language or to the runtime, depending on the sentence:
- Python the language defines how valid Python code is written.
- A Python runtime is the installed or browser-based program that runs that code.
The distinction explains why code can exist as a text file even when Python is not installed, and why the same source code can run in the browser or on your computer when a compatible runtime is available.
Why We Use Python Here
Python makes small computations easy to inspect. Its machine-learning and scientific libraries are widely used, but this book does not begin by hiding ideas inside those libraries.
We will first use short programs whose values and outputs remain visible. Later, NumPy and machine-learning frameworks will express larger computations more compactly. The earlier programming habits will still matter when those computations produce an unexpected shape, value, or error.
Instructions Must Be Explicit
A computer does not fill in an unstated intention. If a program should ignore a missing reading, round an average, or reject an impossible temperature, its instructions must describe that behavior.
This does not mean that every detail must be solved at once. Programmers often begin with one small, clear case, observe the result, and then add the next requirement.
A program receives a list of quiz scores, computes their average, and displays the average. Which option correctly identifies its input, processing, and output?
Select one choice, then check.
HintFollow the information
Begin with the scores before the program acts on them. Then identify the calculation and the result made visible at the end.
SolutionInput, processing, and output
The quiz scores are the input. Computing their average is the processing. The displayed average is the output.
The Mental Model to Keep
A programmer starts with a task, breaks it into a method, expresses the method as source code, and uses a runtime to execute that code. The resulting output provides evidence about what the program actually did.
The next lesson begins that process with a few lines of Python.