Python Tutorial Part 1 > The Python Programming Language
The Python Programming Language
The programming language you will be learning 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.
As you might infer from the name 'high-level language,' there are also low-
level languages, sometimes referred to as 'machine languages' or 'assembly
The way of the program
languages.' Loosely speaking, computers can only execute programs written in
low-level languages. Thus, 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.
But the advantages are enormous. First, it is much easier to program in a highlevel
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.
Due to these advantages, almost all programs are written in high-level languages.
Low-level languages are used only for a few specialized applications.
Two kinds of programs process high-level languages into low-level languages: in-
terpreters 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 case, 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: command-line
mode and script mode. In command-line mode, you type Python programs and
the interpreter prints the result:
Python 2.4.1 (#1, Apr 29 2005, 00:28:56)
Type "help", "copyright", "credits" or "license" for more information.
>>> print 1 + 1
The first line of this example is the command that starts the Python interpreter.
The next two lines are messages from the interpreter. The third line starts with
>>>, which is the prompt the interpreter uses to indicate that it is ready. We
typed print 1 + 1, and the interpreter replied 2.
Alternatively, you can write a program in a file and use the interpreter to execute
the contents of the file. Such a file is called a script. For example, we used a text
editor to create a file named latoya.py with the following contents:
print 1 + 1
By convention, files that contain Python programs have names that end with .py.
To execute the program, we have to tell the interpreter the name of the script:
$ python latoya.py
In other development environments, the details of executing programs may differ.
Also, most programs are more interesting than this one.
Most of the examples in this book are executed on the command line. Working
on the command line is convenient for program development and testing, because
you can type programs and execute them immediately. Once you have a working
program, you should store it in a script so you can execute or modify it in the
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
sequence of statements.
repetition: Perform some action repeatedly, usually with some variation.
Believe it or not, that's pretty much all there is to it. Every program you've ever
used, no matter how complicated, is made up of instructions that look more or
less like these. Thus, we can describe programming as the process of breaking a
large, complex task into smaller and smaller subtasks until the subtasks are simple
enough to be performed with one of these basic instructions.
That may be a little vague, but we will come back to this topic later when we talk
What is debugging?
Programming is a complex process, and because it is done by human beings, it
often leads to errors. For whimsical reasons, programming errors are called bugs
and the process of tracking them down and correcting them is called debugging.
Three kinds of errors can occur in a program: syntax errors, runtime errors, and
semantic errors. It is useful to distinguish between them in order to track them
down more quickly.
Python can only execute a program if the program is syntactically correct; otherwise,
the process fails and returns an error message. Syntax refers to the
structure of a program and the rules about that structure. For example, in English,
a sentence must begin with a capital letter and end with a period. this
sentence contains a syntax error. So does this one
For most readers, a few syntax errors are not a significant problem, which is why
we can read the poetry of e. e. cummings without spewing error messages. Python
is not so forgiving. If there is a single syntax error anywhere in your program,
Python will print an error message and quit, and you will not be able to run
your program. During the first few weeks of your programming career, you will
probably spend a lot of time tracking down syntax errors. As you gain experience,
though, you will make fewer errors and find them faster.
The second type of error is a runtime error, so called because the error does
not appear until you run the program. These errors are also called exceptions
because they usually indicate that something exceptional (and bad) has happened.
Runtime errors are rare in the simple programs you will see in the first few chapters,
so it might be a while before you encounter one.
The third type of error is the semantic error. If there is a semantic error in your
program, it will run successfully, in the sense that the computer will not generate
any error messages, but it will not do the right thing. It will do something else.
Specifically, it will do what you told it to do.
The problem is that the program you wrote is not the program you wanted to
write. The meaning of the program (its semantics) is wrong. Identifying semantic
errors can be tricky because it requires you to work backward by looking at the
output of the program and trying to figure out what it is doing.
One of the most important skills you will acquire is debugging. Although it can
be frustrating, debugging is one of the most intellectually rich, challenging, and
interesting parts of programming.
In some ways, debugging is like detective work. You are confronted with clues,
and you have to infer the processes and events that led to the results you see.
Debugging is also like an experimental science. Once you have an idea what is
going wrong, you modify your program and try again. If your hypothesis was
correct, then you can predict the result of the modification, and you take a step
closer to a working program. If your hypothesis was wrong, you have to come up
with a new one. As Sherlock Holmes pointed out, 'When you have eliminated
the impossible, whatever remains, however improbable, must be the truth.' (A.
Conan Doyle, The Sign of Four)
For some people, programming and debugging are the same thing. That is, programming
is the process of gradually debugging a program until it does what you
want. The idea is that you should start with a program that does something and
make small modifications, debugging them as you go, so that you always have a
For example, Linux is an operating system that contains thousands of lines of
code, but it started out as a simple program Linus Torvalds used to explore the
Intel 80386 chip. According to Larry Greenfield, 'One of Linus's earlier projects
was a program that would switch between printing AAAA and BBBB. This later
evolved to Linux.' (The Linux Users' Guide Beta Version 1)
Later chapters will make more suggestions about debugging and other programming
Formal and natural languages
Natural languages are the languages that people speak, such as English, Spanish,
and French. They were not designed by people (although people try to impose
some order on them); they evolved naturally.
Formal languages are languages that are designed by people for specific applications.
For example, the notation that mathematicians use is a formal language
that is particularly good at denoting relationships among numbers and symbols.
Chemists use a formal language to represent the chemical structure of molecules.
And most importantly:
Programming languages are formal languages that have been
designed to express computations.
Formal languages tend to have strict rules about syntax. For example, 3 + 3 = 6
is a syntactically correct mathematical statement, but 3=+6$ is not. H2O is a
syntactically correct chemical name, but 2Zz is not.
Syntax rules come in two flavors, pertaining to tokens and structure. Tokens are
the basic elements of the language, such as words, numbers, and chemical elements.
One of the problems with 3=+6$ is that $ is not a legal token in mathematics (at
least as far as we know). Similarly, 2Zz is not legal because there is no element
with the abbreviation Zz.
The second type of syntax error pertains to the structure of a statement'that
is, the way the tokens are arranged. The statement 3=+6$ is structurally illegal
because you can't place a plus sign immediately after an equal sign. Similarly,
molecular formulas have to have subscripts after the element name, not before.
As an exercise, create what appears to be a well-structured English
sentence with unrecognizable tokens in it. Then write another sentence
with all valid tokens but with invalid structure.
When you read a sentence in English or a statement in a formal language, you
have to figure out what the structure of the sentence is (although in a natural
language you do this subconsciously). This process is called parsing.
For example, when you hear the sentence, 'The other shoe fell,' you understand
that 'the other shoe' is the subject and 'fell' is the predicate. Once you have
parsed a sentence, you can figure out what it means, or the semantics of the
sentence. Assuming that you know what a shoe is and what it means to fall, you
will understand the general implication of this sentence.
Although formal and natural languages have many features in common'tokens,
structure, syntax, and semantics'there are many differences:
ambiguity: Natural languages are full of ambiguity, which people deal with by
using contextual clues and other information. Formal languages are designed
to be nearly or completely unambiguous, which means that any statement
has exactly one meaning, regardless of context.
redundancy: In order to make up for ambiguity and reduce misunderstandings,
natural languages employ lots of redundancy. As a result, they are often
verbose. Formal languages are less redundant and more concise.
literalness: Natural languages are full of idiom and metaphor. If I say, 'The
other shoe fell,' there is probably no shoe and nothing falling. Formal
languages mean exactly what they say.
People who grow up speaking a natural language'everyone'often have a hard
time adjusting to formal languages. In some ways, the difference between formal
and natural language is like the difference between poetry and prose, but more so:
Poetry: Words are used for their sounds as well as for their meaning, and the
whole poem together creates an effect or emotional response. Ambiguity is
not only common but often deliberate.
Prose: The literal meaning of words is more important, and the structure contributes
more meaning. Prose is more amenable to analysis than poetry but
still often ambiguous.
Programs: The meaning of a computer program is unambiguous and literal, and
can be understood entirely by analysis of the tokens and structure.
Here are some suggestions for reading programs (and other formal languages).
First, remember that formal languages are much more dense than natural languages,
so it takes longer to read them. Also, the structure is very important, so
it is usually not a good idea to read from top to bottom, left to right. Instead,
learn to parse the program in your head, identifying the tokens and interpreting
the structure. Finally, the details matter. Little things like spelling errors and
bad punctuation, which you can get away with in natural languages, can make a big difference in a formal language.
The first program
Traditionally, the first program written in a new language is called 'Hello, World!'
because all it does is display the words, 'Hello, World!' In Python, it looks like
print "Hello, World!"
This is an example of a print statement, which doesn't actually print anything
on paper. It displays a value on the screen. In this case, the result is the words
The quotation marks in the program mark the beginning and end of the value;
they don't appear in the result.
Some people judge the quality of a programming language by the simplicity of
the 'Hello, World!' program. By this standard, Python does about as well as