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

Getting Started

What will we cover?

How to start Python and what an error message looks like - just in case...

For the next set of exercises I will assume you have a properly installed version of Python on your computer. If not, go fetch the latest version from the Python web site and follow the install instructions for your platform.

Now from a command prompt type python and the Python prompt should appear looking something like this:

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32

Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam

>>>

A word about error messages

If you type in the commands as we go through them then sooner or later you will get an error message. It will look something like this:

>>> print 'fred' + 7 Traceback (innermost last):

File "", line 1, in ?

TypeError: illegal argument type for built-in operation

Don't worry about the exact meaning here just look at the structure. The '>>> print ...' line is the erroneous command

The next 2 lines are describing where the error occurred

- 'line 1 in ?' means line 1 in the command we are typing. If it were a longer program stored in a source file the question mark would be replaced by the file name.

The 'TypeError...' line tells you what the interpreter thinks is wrong and sometimes there will be a caret character(^) pointing to the part of the line that Python thinks is at fault.

Unfortunately this will often be wrong - remember computers are dumb!

Use the error information to figure out what's happening. Remember it's most likely to be you at fault not the computer. Remember too that computers are dumb. Probably you just mistyped something or forgot a quote sign or something similar. Check carefully.

In case you are wondering, the mistake I made was trying to add a number to a character string. You're not allowed to do that so Python objected and told me there was a TypeError. You'll need to wait till we get to the bit about 'Data' to understand what types are all about....

Now we are ready to start creating some very simple Python programs.

Points to remember

Start python by typing python at a command prompt

Error messages are nothing to be scared of, read them carefully, they usually give a clue as to why you got them.

But it's only a clue... if in doubt check the lines immediately before the reported line.

15