Friday, December 7, 2012

Hello World!

The first thing we write when we are learning a new programming language is normally a 'Hello World' programme. This is nice, easy (and short!) and gives us a quick way to see how the language works. All it does is print 'Hello World' onto the screen. Let's try. If you are using a Raspberry Pi, double click on the icon called 'IDLE' on the screen. This should bring up a window called 'Python Shell'. Click next to the three little arrows ('>>>') and type the following, exactly as it is written (don't type the three arrows again though!):

>>> print 'Hello world'

Hit the Enter key (also sometimes called the Return key) and see what happens. You should see python say:

Hello world

at you. If it didn't work, python may say something strange, such as:

Syntax Error: Invalid Syntax

This is python's way of saying it thinks you made a spelling mistake, or didn't type something quite right. If this happened, look very carefully at what you typed. Did you remember to put quotes (the little ' marks) round 'hello world'?

Another thing python might say is:

File "", line 1 print 'Hello world' ^ IndentationError: unexpected indent

If you see this, check you didn't put an extra space in before typing.

Python (and lots of other programming languages) may be very good at doing things, but they have to be told exactly what to do. If you spell a command (like 'print') wrongly, Python won't be able to guess what you meant.

Let's see what you actually told the computer to do. First you told the computer that you wanted it to print something onto the screen, then you told it what you wanted to print ('Hello World'). If you think about when you read a book, you know when someone starts saying something, and when they stop by using quotes or speech marks. Python and other programming languages do the same.

So we got the computer to talk to us, which is nice, but wouldn't be nice to make it a bit more personal? That's what we'll look at in the next episode

No comments:

Post a Comment