Chapter 1
Programmable software is what makes a computer a powerful tool. Each different program essentially “rewires” the computer to allow it to perform a different task. In this course, you will learn basic principles of writing software in the Python programming language.
Python is a popular scripting language available as a free download from http://www.python.org/. Follow the instructions given there to install the latest production version of Python 3 on your system. The examples in this text were written with Python 3.2.
In order to write software, it will be helpful to be able to imagine what happens inside a computer when your program runs. We begin with a rough picture and gradually fill in details along the way.
When a program is ready to run, it is loaded into RAM, usually from long-term storage such as a hard drive. RAM is an acronym for random access memory, which is the working memory of a computer. RAM is volatile, meaning that it requires electricity to keep its contents.
Once a program is loaded into RAM, the CPU, or central processing unit, executes the instructions of the program, one at a time. Each type of CPU has its own instruction set, and you might be surprised at how limited these instruction sets are. Most instructions boil down to a few simple types: load data, perform arithmetic, and store data. What is amazing is that these small steps can be combined in different ways to build programs that are enormously complex, such as games, spreadsheets, and physics simulations.
CPU instruction sets are also known as machine languages. The key point to remember about machine languages is that in order to be run by a CPU, a program must be written in the machine language of that CPU. Unfortunately, machine languages are not meant to be read or written by humans. They are really just specific sequences of bits in memory. (We will explain bits later if you do not know what they are.)
Because of this, people usually write software in a higher-level language:
Level |
Language |
Purposes |
---|---|---|
Higher |
Python |
Scripts |
|
Java |
Applications |
|
C, C++ |
Applications, Systems |
|
Assembly Languages |
Specialized Tasks |
Lower |
Machine Languages |
|
This table is not meant to be precise, but, for example, most programmers would agree that C and C++ are closer to the machine than Python.
Now if CPUs can only run programs written in their own machine language, how do we run programs written in Python, Java, or C++? The answer is that the program must be translated into machine language first.
There are two main types of translation: compilation and interpretation. When a program is compiled, it is completely translated into machine language, producing an executable file. C and C++ programs are usually compiled, and when they are compiled in Microsoft Windows®, for example, the executable files generally end in .exe.
On the other hand, when a program is interpreted, it is translated “on-the-fly.” No separate executable file is created. Instead, the translator program (the interpreter) is running and it translates your program so that the CPU can execute it. Python programs are usually interpreted.
When you start Python, you are in immediate contact with a Python interpreter. If you provide it with legitimate Python, the interpreter will translate your code so that it can be executed by the CPU. The interpreter displays the version of Python that it will interpret and then shows that it is ready for your input with this prompt:
>>>
The interpreter will translate and execute any legal Python code that is typed at the prompt. For example, if we enter the following statement:
>>> print("Hello!")
the interpreter will respond accordingly. Try it and see.
Remember that as you learn new Python constructs, you can always try them out in the interpreter without having to write a complete program. Experiment—the interpreter will not mind.
Still, our focus will be on writing complete Python programs. Here is a short example:
Listing 1.1: Area of a Circle
1 from math import pi
2 r = 12
3 area = pi * r ** 2
4 print("The area of a circle with radius", r, "is", area)
Even if you have never seen Python before, you can probably figure out what this program does.
Start the Python IDLE application and choose “New Window” from the File menu. Type Listing 1.1 into the new window that appears. Save it as circle.py, and then either choose “Run Module” from the Run menu or press F5 to run the program. This program illustrates many important Python concepts, including variables, numeric expressions, assignment, output, and using the standard library. We will examine these components in the next chapter.
Here are a few things to consider as we begin:
The PCC 2000 consists of a 3MHz 8085A microprocessor, two 32K memory boards, two FD514 double-density, 8.5-inch floppy disk drives, a 12-inch upper/lower case video display. . .
3.145.78.155