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.6.2 (default, Jul 29 2017, 00:00:00) [GCC 4.8.4] on linux Type "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. The tutorial duplicates some of the early material here but repetition of the basics is no bad thing!
If you enjoy video training then there are many programming related videos on YouTube. My personal take on video training is that you can learn concepts and tool usage from a video but unless you pause the video after every other line of code you won't be able to follow along. And typing in the code and running it for yourself is what makes you remember what you are learning. So by all means watch a video to get the concepts but then come back and read about it, and type the code in and try it out. Then modify it until you can predict what your changes do. It's the only real way to learn to program.
The official 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 PyWin32 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 OS prompt, but at the very beginning I prefer to use the most basic tools to focus on the underlying principles rather than the toys.
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 note
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 hold down the Windows key and hit r (for 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.
Unfortunately some recent versions of Python have not apparently been setting things up such that the CMD prompt can find Python. If this is the case with you then you need to set an Environment Variable called Path.
Assuming you are using Windows 8, or higher, you do that by holding down the Windows key and hitting e (for Explorer). In the window that appears, locate the This PC entry and right click to select Properties. Go to Advanced system settings and in the tool that appears go to the Advanced tab. Click the Environment Variables button at the bottom.
You should find that Path is already defined as a System variable so select it and click Edit. Be very careful in the next step not to delete the existing path by over-typing, if this does happen then press Escape or use the Cancel button to exit the dialog and try again.
In the Variable Value field go to the end and add a semicolon (;) and the full path to your Python executable folder. OK your way back out. (If you don't know the full path to Python use the Explorer search tool to search for the file python3.exe and the contents of the "In Folder" column of the search screen is the full path). You may find you need to reboot to get the changes to take effect, this seems to differ between Windows versions.
Now you can type python3 at the command prompt from any folder and it should start python. So you can CD (Change Directory) to the folder holding your script and type the command as per the tutorial. There is a list of CMD commands that you can type at the command prompt in the CMD help system. Just type help at the command prompt. For help on how to use any given command type the name of the command followed by /?. So for help on the DIR command that we used above, type:
C:\WINDOWS> DIR /?
Finally you can create a desktop short-cut by simply right-clicking the Desktop, selecting New->Short-cut. In the dialog that appears type cmd in the location box 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.
Take time to explore this powerful tool. It may seem primitive but it can be much more powerful, especially when doing repetitive tasks, than the GUI tools like Windows Explorer. A good place to start is the tutorial at Computer Hope.
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 occurred. 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 just as 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.
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 the software engineering vocabulary.)
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 - including the new Windows 10 browser Edge. It looks like VBScript's days will soon be numbered.
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. (There are some sophisticated debugging tools built into most modern web browsers but they are carefully hidden away and are targeted at professional developers so not really suitable for our needs just yet!)
OK, Whichever language you choose you are ready to start.