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 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Alternatively you might find a shortcut to something called IDLE, or the Python GUI, in your start menus. If you start IDLE instead of the command line version you will get a similar prompt but in a window of its own and with some pretty font colors! Danny Yoo has written a pretty good IDLE Tutorial to get you started with IDLE and I recommend you pay it a visit if you want to stick with it rather than the basic command prompt. It duplicates some of the early material here but repetition of the basics is no bad thing!

The full manual for IDLE is found here. For now I'd recommend you stick with Danny's tutor.

One interesting thing about IDLE is that it is itself a Python program, so it's a very good demonstration of just what is possible using Python

If you got your version of Python from ActiveState or if you downloaded the Windows specific extensions (the winall package), you also have access to another GUI programming environment, very similar to IDLE but perhaps a little more polished, called Pythonwin. Either Pythonwin or IDLE make far better programming environments than the standard DOS prompt, but at the very beginning I prefer to use the most basic tools to focus on the underlying principles rather than the toys.

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 (most recent call last):
  File "<input>", line 1, in ?
TypeError: cannot concatenate 'str' and 'int' objects

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 <input> 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, usually the error is earlier in the line, or even in the (one or two) lines immediately preceding where Python says it is - 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 again 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 topic on the Raw Materials to understand what types are all about....

Whichever approach you've decided to take, command prompt or IDLE (or Pythonwin) we are ready to start creating some very simple Python programs.

JavaScript

To create JavaScript programs in a browser we need to do a bit more work. We need to create an HTML file which we can load into a web browser. The file will look like this:

<html>
<body>

<script language="JavaScript">

document.write('Hello World\n');

</script>

</body>
</html>

The bit between <script...> and </script> is our program. I won't be showing all the HTML tags every time in this tutorial so you need to copy that file each time as a template and then replace everything between the script tags with the code you want to try out.

VBScript

VBScript is essentially the same as JavaScript with the single difference that you replace the name "JavaScript" in the language= bit with, surprisingly enough, "VBScript". Like this:

<html>
<body>

<script language="VBScript">

MsgBox "Hello World"

</script>

</body>
</html>

Once again the bit between the <script> tags is the program.

VBScript and JavaScript errors

In both VBScript and JavaScript you will get a dialogue box pop up telling you the line number of an error. There will also be a fairly inscrutable error message. As with Python, treat the line number as a rough guide rather than an exact pointer. After finding and fixing the error you will need to reload (or refresh) the page in your browser.

OK, Whichever language you choose you are ready to start.

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.
Previous  Next  Contents


If you have any questions or feedback on this page send me mail at: alan.gauld@btinternet.com