|What will we cover?|
IMPORTANT NOTE: Although I use 3 languages you are not expected to type in all of the exercises in all 3 languages. That would just be confusing. You might want to try all of them in the early examples just to get experience of using them. However, as you get beyond the first couple of topics you should choose one (I recommend Python, but you can choose one of the others if you prefer) and type those examples in, then simply read through the other language examples to note the similarities and differences between them. That way you should get a feel for both reading code (an important skill for a programmer in itself) and understanding the common ideas between all programming languages.
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. (If you are a Windows user and don't know what a command prompt is or how to start one then read the sidebar sidebar below. I assume Linux users will be familiar with terminal windows, and MacOS users just have an icon to click as usual.) 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.
The Windows Command Prompt
Since I first wrote this tutorial it has become obvious
that many Windows users are no longer familiar with the
venerable MS Dos command prompt. This sidebar will describe
how this invaluable tool can be accessed and provide the
bare minimum on how to use it.
There are several ways to access the command prompt. The simplest way is to go to the Start Menu and select Run.... In the dialog that appears type cmd and hit OK. A black window should appear with a white textual prompt saying something like:
This indicates the folder where the user is now located and typing DIR and hitting return should display a list of all the files in the directory. You can also try typing python and if all is well you should see the familiar >>> prompt and be able to type Python commands.
C:\WINDOWS> DIR /?
Finally you can create a desktop shortcut by simply right-clicking the Desktop, selecting New->Shortcut. In the wizard that appears type cmd and click Next > then change the name to something like "Command Prompt" or similar. Click Finish and a new icon should appear on the desktop for you to click.
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.
Basically there are three types of error you will encounter. The first of these is a syntax error which basically means that Python didn't recognise what you typed as a valid program, it's like missing out punctuation in an English sentence or spelling a word wrongly. The next category of errors is a runtime error which is where the program looks valid but for some reason cannot be executed, you have tried to perform an illegal, or unavailable operation. Trying to read a file that did not exist would be an example. The final type of error is a semantic error which is where the program appears to have worked in that there are no error messaages but it produces the wrong output. For example if you wrote a word counter program that reported that this file only contained 5 words, it would obviously be wrong, so the program has a semantic error. Most of the errors you will come across initially will be syntax errors or runtime errors, later, as you start to design your own programs, you will make semantic errors and need to fix them. This process is called debugging and the errors so found are called bugs for historic reasons - one of the first computer faults was due to a real insect-type bug getting stuck inside the machine!
In case you are wondering, the mistake I made was trying to add
a number to a character string which is a semantic error but
in this case is one which also generates a runtime error..
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.
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.
<html> <body> <script type="text/vbscript"> MsgBox "Hello World" </script> </body> </html>
Once again the bit between the <script> tags is the program.
OK, Whichever language you choose you are ready to start.
|Points to remember|
If you have any questions or feedback on this page send me mail at: firstname.lastname@example.org