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. (Note that python is available for most computer types and there are pre-built installers for both 32 and 64 bit Windows as well as MacOS and the popular Linux distributions (via your favourite package manager). If in doubt install the 32 bit version and it should work just fine on 64 bit machines too)
Now from an operating system 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 box below. I assume Linux users will be familiar with terminal (or Console) windows, and MacOS users can start the Terminal application by clicking the icon as usual.) The Python prompt should appear looking something like this:
Python 3.0 (r30:67503, Dec 11 2008, 09:05:16) [MSC v.1500 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. IDLE is a dedicated programming environment for Python and provides lots of helpful tools and commands that are useful to programmers. 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 prettier 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! Another source of useful information is the set of video tutorials on the ShowMeDo web site. In particular take a look at the IDLE and Command Line videos, although not all of them are free! (Note: These guys add new material from time to time and its well worth checking out occasionally, just to see what's there.)
The full manual for IDLE is found here. For now I'd recommend you stick with Danny's tutorial.
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 may 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 PromptSince 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: C:\WINDOWS> 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 program statements 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 statement
The next 2 lines are describing where the error occurred. This could be
many more lines long in more complex programs because as the word
Traceback suggests it includes a trace, or record, of everything
your program was doing at the time the error occured. This can be intimidating
to a beginner but as you gain experience, trust me, you will be glad it is there.
The trick is to read it from the bottom up as just far as is needed.
The 'line 1 in ?' means line 1 in the statement we are typing.
If it were a longer program stored in a source file the <input>
would be replaced by the real 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 while computers are dumb, they are accurate! 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 messages 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 - allegedly one of the first computer faults was due to a real insect-type bug getting stuck inside the machine! (The usage of the word bug for a defect did not actually originate there and had been used in traditional engineering for many decades prior to this, but the incident seems to have cemented the term into software engineering language.)
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.
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. An HTML file is really just a plain text file which you can create in Notepad or any other text editor. Save it with an extension of .htm or .html. The file will look like this:
<html> <body> <script type="text/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.
Finally, open it in your favourite web browser by clicking it in your file manager program. The Operating System should automatically start a browser and load the file which will in turn cause your program to execute.
VBScript is essentially the same as JavaScript with the single difference that you replace the name "javascript" in the type= bit with, surprisingly enough, "vbscript". Like this:
<html> <body> <script type="text/vbscript"> MsgBox "Hello World" </script> </body> </html>
Once again the bit between the <script> tags is the program. Note that only Microsoft Internet Explorer will be able to run VBScript, other browsers tend to only support JavaScript.
In both VBScript and JavaScript you will get a dialog 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 web browser.
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:
alan.gauld@yahoo.co.uk