Earlier,
we gave two definitions for Python: it is a language for writing programs, and
a program that reads instructions and executes them in order. In this section
we expand on that idea in more detail.
Programming
Languages, fundamentally, are ways of letting you write in language that is
almost natural, and making that almost-natural language into instructions for
computers.
Every
programming language, then, is two things: First, it is a language, in the sense that there is syntax, a right and wrong way
to write it. You do not say "house the I visited" in English, you
say, "I visited the house". You do not say, "17 + 5 print"
in Python, you say, "print 17 + 5".
Second,
every programming language is itself
a program for turning written code into computer instructions. You hand Python
what is, basically, a text file with some words that you wrote, and Python
makes the computer do stuff!
DEFINITION: Machine
instructions are instructions for computer hardware. In most cases, they
directly instruct the computer's central processing unit, or CPU, to do things.
Most
machine instructions are very basic actions, like arithmetic and memory
retrieval, and are conducted in binary.
Binary is the sequence of 1s and 0s that almost all computers use to do almost
everything.
There
are two basic types of programs for turning written code into machine
instructions. The first type is called a compiler.
A compiler does the whole conversion at once, giving you a file with computer
code (in Windows, this file is typically an exe file; in OS X, it is typically
hidden as the core of an app). One advantage of a compiler is speed. The
conversion to machine code can be highly optimized, so that the resulting set
of machine instructions is fast.
The
second type is called an interpreter.
An interpreter does the conversion a little bit at a time, while the program is
running. So, if you write a program that is a hundred lines long, the
interpreter might grab a dozen or so lines at a time, and quietly convert these
into instructions for your computer. Two advantages of an interpreter are
portability and rapid development. If you write a program on a Windows
computer, and share it with someone on an OS X computer, you can each (usually,
mostly) run the same program, each with your own interpreter. By contrast,
compiled code is specific to one type of machine. Windows can't easily run
compiled code from OS X, or vice versa (though this is easier now than it used
to be). So interpreted code is more portable.
Interpreted
code is also typically faster to develop. When you make small mistakes in a
compiled language, there is a long turnaround time, as you need to re-compile
the program. When you make mistakes in an interpreted language, you can usually
re-run the program with your fixes immediately.
Python
is an interpreted language. So, there is a Python interpreter that runs your
programs. It converts your code (the text file itself) into machine
instructions, a few lines at a time.
It
is useful, when talking with other programmers, to be comfortable with the
different meanings of the word Python.
So, review these phrases: Python code is what you write into a text file. The Python language is the set of rules (like grammar) for writing a Python program. The Python program or interpreter is what turns your text file into computer instructions, and makes your computer do those instructions.