Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gauld A.Learning to program (Python)_1.pdf
Скачиваний:
23
Добавлен:
23.08.2013
Размер:
1.34 Mб
Скачать

>>>import sys

Now this is a strange one. If you've tried it you'll see that it apparently does nothing. But that's not really true. To understand what happened we need to look at the architecture of Python (for non Python programmers, bear with me there will be a similar mechanism available to you too!)

When you start Python there are a bunch of commands available to you called built-ins, because they are built in to the Python core. However Python can extend the list of commands available by incorporating extension modules. - It's a bit like buying a new tool in your favourite DIY shop and adding it to your toolbox. The tool is the sys part and the import operation puts it into the toolbox.

In fact what this command does is makes available a whole bunch of new 'tools' in the shape of Python commands which are defined in a file called 'sys.py'. This is how Python is extended to do all sorts of clever things that are not built in to the basic system. You can even create your own modules and import and use them, just like the modules provided with Python when you installed it.

So how do we use these new tools?

>>>sys.exit()

Whoops! What happened there? Simply that we executed the exit command defined in the sys module. That command causes Python to exit. (Note: Normally you exit Python by typing the End Of File(EOF) character at the >>> prompt - CTRL-Z on DOS or CTRL-D on Unix)

Notice that exit had 2 brackets after it. That's because exit is a function defined in sys and when we call a Python function we need to supply the parentheses even if there's nothing inside them!

Try typing sys.exit without the brackets. Python responds by telling you that exit is a function rather than by executing it!

One final thing to notice is that the last two commands are actually only useful in combination. That is, to exit from Python other than by typing EOF you need to type:

import sys sys.exit()

This is a sequence of two commands! Now we're getting closer to real programming....

Using Tcl

We can also type simple commands like this into Tcl too. The Tcl interpreter is started by typing tclsh80 (assuming you have Tcl v8.0)at a DOS prompt. The command prompt is a '%' sign. Try the following examples:

%put "Hello world" Hello world

%put [expr 4 + 6] 10

Note that in the last example the section in square brackets is evaluated first and the result passed to the put command. Unlike Python you can't assume the put will attempt to interpret what you mean, it expects a character string and it's up to you to ensure it gets one.

To exit from tclsh80 just type exit at the prompt

18

And BASIC too...

To start BASIC type QBASIC at the DOS prompt. In this case a whole programming environment will start. Get rid of the welcome dialog etc and in the edit window you can type commands and then run whatever is in the window using the Run menu. This has the advantage that the environment allows you to edit the commands, and even does some checks on the text as you enter it.

Enter the following set of commands and then Run them:

PRINT "Hello world"

PRINT 4+6

You exit as usual via the File|Exit menu option.

That's our first look at programming, it wasn't too painful was it? Before we continue tho' we need to take a look at the raw materials of programming, data and what we can do with it.

Points to remember

Even a single command is a program

Python does math almost the way you'd expect

To get a fractional result you must use a fractional number

You can combine text and numbers using the % format operator

Quit with import sys; sys.exit()

19