The way of the program
The goal of my tutorial is to teach you to think like a computer scientist. This way of thinking combines some of the best features of mathematics, engineering, and natural science.
Like mathematicians, computer scientists use formal languages to denote ideas (specifically computations). Like engineers, they design things, assembling components into systems
and evaluating trade offs among alternatives. Like scientists, they observe the behavior of complex systems, form hypotheses, and test predictions.
The single most important skill for a computer scientist is problem solving. Problem solving
means the ability to formulate problems, think creatively about solutions, and express
a solution clearly and accurately. As it turns out, the process of learning to program is an
excellent opportunity to practice problem-solving skills. That’s why this chapter is called,
“The way of the program.”
On one level, you will be learning to program, a useful skill by itself. On another level, you
will use programming as a means to an end. As we go along, that end will become clearer.
The Python programming language
The programming language you will learn is Python. Python is an example of a high-level
language; other high-level languages you might have heard of are C, C++, Perl, and Java.
There are also low-level languages, sometimes referred to as “machine languages” or “assembly
languages.” Loosely speaking, computers can only run programs written in low level languages. So programs written in a high-level language have to be processed before they can run. This extra processing takes some time, which is a small disadvantage of high-level languages.
The advantages are enormous. First, it is much easier to program in a high-level language.
Programs written in a high-level language take less time to write, they are shorter and easier to read, and they are more likely to be correct. Second, high-level languages are portable, meaning that they can run on different kinds of computers with few or no modifications.
Low-level programs can run on only one kind of computer and have to be rewritten
to run on another.
A compiler translates source code into object code, which is run by a hardware
executor.
Due to these advantages, almost all programs are written in high-level languages. Lowlevel
languages are used only for a few specialized applications.
Two kinds of programs process high-level languages into low-level languages: interpreters
and compilers. An interpreter reads a high-level program and executes it, meaning that it
does what the program says. It processes the program a little at a time, alternately reading
lines and performing computations.
A compiler reads the program and translates it completely before the program starts running.
In this context, the high-level program is called the source code, and the translated
program is called the object code or the executable. Once a program is compiled, you
can execute it repeatedly without further translation.
Python is considered an interpreted language because Python programs are executed by an
interpreter. There are two ways to use the interpreter: interactive mode and script mode.
In interactive mode, you type Python programs and the interpreter displays the result:
>>> 1 + 1
2
The chevron, >>>, is the prompt the interpreter uses to indicate that it is ready. If you type
1 + 1, the interpreter replies 2.
Alternatively, you can store code in a file and use the interpreter to execute the contents of
the file, which is called a script. By convention, Python scripts have names that end with
.py
To execute the script, you have to tell the interpreter the name of the file. If you have a
script named dinsdale.py and you are working in a UNIX command window, you type
python dinsdale.py. In other development environments, the details of executing scripts
are different. You can find instructions for your environment at the Python website http://python.org
Working in interactive mode is convenient for testing small pieces of code because you can
type and execute them immediately. But for anything more than a few lines, you should
save your code as a script so you can modify and execute it in the future.
What is a program?
A program is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of equations or finding the roots of a polynomial, but it can also be a symbolic computation, such as searching and replacing text in a document or (strangely enough) compiling a program.
The details look different in different languages, but a few basic instructions appear in just
about every language:
input: Get data from the keyboard, a file, or some other device.
output: Display data on the screen or send data to a file or other device.
math: Perform basic mathematical operations like addition and multiplication.
conditional execution: Check for certain conditions and execute the appropriate code.
repetition: Perform some action repeatedly, usually with some variation.
the Glossary
problem solving: The process of formulating a problem, finding a solution, and expressing
the solution.
high-level language: A programming language like Python that is designed to be easy for
humans to read and write.
low-level language: A programming language that is designed to be easy for a computer
to execute; also called “machine language” or “assembly language.”
portability: A property of a program that can run on more than one kind of computer.
interpret: To execute a program in a high-level language by translating it one line at a time.
compile: To translate a program written in a high-level language into a low-level language
all at once, in preparation for later execution.
source code: A program in a high-level language before being compiled.
object code: The output of the compiler after it translates the program.
executable: Another name for object code that is ready to be executed.
prompt: Characters displayed by the interpreter to indicate that it is ready to take input
from the user.
script: A program stored in a file (usually one that will be interpreted).
interactive mode: A way of using the Python interpreter by typing commands and expressions
at the prompt.
script mode: A way of using the Python interpreter to read and execute statements in a
script.
program: A set of instructions that specifies a computation.
algorithm: A general process for solving a category of problems.
bug: An error in a program.
debugging: The process of finding and removing any of the three kinds of programming
errors.
syntax: The structure of a program.
syntax error: An error in a program that makes it impossible to parse (and therefore impossible
to interpret).
exception: An error that is detected while the program is running.
semantics: The meaning of a program.
semantic error: An error in a program that makes it do something other than what the
programmer intended.
natural language: Any one of the languages that people speak that evolved naturally.
formal language: Any one of the languages that people have designed for specific purposes,
such as representing mathematical ideas or computer programs; all programming
languages are formal languages.
token: One of the basic elements of the syntactic structure of a program, analogous to a
word in a natural language.
parse: To examine a program and analyze the syntactic structure.
print statement: An instruction that causes the Python interpreter to display a value on
the screen.
0 comments:
Post a Comment