The traditional first program you write when learning any programming language is a "Hello, world!" program, that simply prints a message on the screen.
So far we have learnt to interact with Python using the IDLE console window. We have used that to perform simple calculations using Python. That is a useful start, but it isn't really programming.
A computer program needs to be able to complete a task automatically, which means that it needs to perform a sequence of operations without any help from the user.
Here we will learn:
We have already seen how to run the IDLE console window, in the previous lesson Using Python as a calculator.
Open up the IDLE console window as before:
IDLE has a second type of window called an edit window. This allows you to type, save and run code.
From the IDLE console window you should choose the File | New File menu item. This will open the new editing window:
You will normally keep both the console window and edit window open, and switch between them as needed:
We are now going to write a Hello, world! program – a simple program that outputs the message "Hello, world!".
Here is the program. Type it in, exactly as shown, in the edit window.
This program only has one line of code. It uses the print function to display some text in the console window.
The text is stored as a string. We met strings in the previous lesson, a string is just some text enclosed in double quote characters, like this:
Note: the text shown uses different colours to make the code easier to read. This is called syntax highlighting.
You just need to type the code in. The editor will apply syntax highlighting automatically (it might not use the same colours as shown above, different editors use different colours, and in IDLE you can configure the colours it uses if you wish).
You can now run your program.
If you get an error message, see the hints later on.
You should now look the console window, where Python displays its results:
Ignore the RESTART message, Python prints that each time it runs a new program.
Our output text is displayed in blue underneath – the text from our print statement: Hello, world!
Like most programming languages, Python expects a program to obey exact rules (called the Python syntax). If not, it will give an error. If your program gave an error instead of the expected result, here are some things to check:
For certain errors, Python will pop up an error window to tell you of the error. It will also mark the line where the error occurred, in the edit window.
For most errors, Python will print out an error message, in the console window. This can be useful in helping you find the error (although sometimes it can be a little cryptic). It will tell you which line the error, so you can look back at the edit window to try to find it.
Now we are going to write a program with more than one line. We will print a bit of information about the system. This is a very simple program, it just contains a list of instructions. Python will execute the instructions, one at a time, in the order they are written. This is called sequencing.
import sys print("This is my first python program") print("that has more than one line!") print("It uses Python version", sys.version)
import statement in the first line is needed because we are going to use a special variable later on that exists in the Python
sys module. A module is just a collection of pre-existing code. We need to tell Python to get the module ready.
Then we print some messages. The third message has two parts, a text string and a value
sys.version that is taken from the
sys module. This value is a string that represents the version of Python you are using.
Run your program. It should display something like this in the console window:
This is my first python program that has more than one line! It uses Python version 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)]
You might recall that variable names can't contain a dot . character. So what is the deal with
sys.version? In fact, this refers to a variable called
version that lives in the
sys module (the module we imported at the start of the code). The dot syntax is used to show this relationship. But don't worry too much about this at the moment, you will learn about it later.
Copyright (c) Axlesoft Ltd 2020