In this lesson:
All of you should write at least one computer program that works.
Most will be comfortable with the idea of using variables.
Some will have tried using loops (control statements).
- Jesse Eisenberg in “The Social Network”
It is traditional that when you start to learn a programming language, you begin with some code which prints: “Hello World!”
First open the Python IDLE (Start > Programs > Python > IDLE).
This will bring up the Python Interpreter. The prompt looks like this:
Python is an interpreted language (like, for instance, Ruby and Lua). The interpreter is a really useful tool for when you just want to type in a command and see how it works. Type this:
When you press enter, you should be rewarded with the output:
You’ll notice a couple of things here. Firstly, the thing we want to print goes in brackets. This is because “print” is a function and functions take data to work on from inside brackets. Secondly, the words we wanted to print were inside quotations marks (single ones would work too).
Before we move on to actually writing a program, why not have a play with the interpreter. It’s great for simple maths. Try just typing in:(Don’t copy the prompt out again – I just included that so it looks like you will see).
If you are trying this out, don’t forget that computers use “*” for multiply and “/” for divide.
Okay, now let’s write a program.
In the menu bar of IDLE, choose: File > New Window. A blank editing window will pop up. First of all, choose: “Save as” and save this in your documents with the name “hello.py”. The extension “.py” is used for python code.
Now type this in:
You have now written a program! The first two lines are comments. The “#” symbol tells the computer to ignore that line, so we put in comments that help us to understand the code. This one is pretty simple, but I want you to get into good habits. There’s no real need to leave a blank line after the comments, but spacing your code out nicely makes it easier to read. To run this program, first press Control-S (to save) and then F5.
We could do with making this program a bit more interactive, using a variable. A variable is a memory location to which we can allocate some data for use in the program. Try this:
The “input” function waits for the user to write something and then press enter. Because we typed: “name = input()”, it stores whatever the user types in the variable “name”. Then when it comes to the print command, we can use the variable again and the contents of that variable will be printed out. There are a couple of things to notice about “print()” here. First, we can print more than one thing. If we want a space in between the items, we can put a comma. If we don’t want a space we can use the plus sign. I did this here because I wanted that full stop to come right after the name – it looks so much better that way.
While we are looking at “print()”, here’s something you can try (no screenshot this time). After you open the speech marks, add this to your code: “\n”. If you did it right, this should make the computer print an empty line before continuing with the rest of what is in the print statement. Technically, this is an “escape” code – the “\” makes the computer do something different with whatever comes next – and “n” tells it to add a “new line”. Try “\t” as well, if you like!
Okay, let’s try one more program. This demonstrates the use of a “while” loop.
The fun bit here is the while statement. This involves a it of logic. The computer carries out the instructions in the indented block after the colon (“:”) “while” 1 is “true”. Now 1 is always “true”, so we have an infinite loop. By the way, some things are false – zero and empty variables for instance. This logic stuff is really useful!
Right, I think that is about all we will have time for. If you get through all this, try writing some other short programs, using input() and print(). If you are feeling confident, you might like to look at some other tutorial posts over on Team Python. This one has some more information about mathematical operators.