Python Programming

Starting Python

Learning Outcomes

On completion of this section you will know:

Go to top

Introduction

The most popular programming languages in general use include Visual Basic, C, C++, Java and Python. Of those Python is the most recent language on the scene, although it has been in existance since the late 1980’s. Currently it is the most popular programming language in New Zealand schools.

Because it is open source you can download it for free.

Go to top

Getting Python

Being a free open source language you simply download the Python installer. To do this you go to the website www.python.org/downloads/release/python-352/

This will bring you to the site shown in Figure 1 below. Here you click on the button Downloads and the rest of the site will guide you to downloading the installation software.

Once the downloading is complete you simply run the installer in order to get Python installed on your computer.

Go to top

Running Python

You run the Python shell in the same way as any other application on your computer. Simply go to the Start-up button and select All Programmes. You should see Python 3.5 as one of the options. Clicking on this opens up the sub folders as shown in Figure 2 below. In order to run the Python shell you click on IDLE(Python 3.5, 32-bit).

The shell is shown below in Figure 3

The command line of the Python shell is indicated by the three greater-than signs. Here you can type in lines of Python code and it will actually run. However, if you want to run the same code a second time you have to retype it – not a good idea.

The alternative is to create a Python script file and, when complete and saved, run the file. This is the method that we will follow throughout this book.

Go to top

Creating a Python script file

To create a Python script file you can use any text editor such as Komodo, Notepad++ or even Notepad itself. Don’t use a word processor such as Word, WordPad, WordPerfect or Google Docs. The reason for this is that those word processors insert their own extra coding into the file which will confuse the Python compiler.

Once you have settled on a text editor, open a new file in it and type in the following code

                        #C1Example.py
                        print("Hello World")
                        print("I am learning Python")
                    

Once your code is entered save the file, ensuring that you give it an extension of .py in order to indicate that it is a Python script file.

Before running the script let us first examine the three lines of Python code shown above. A programme is a set of instructions to the computer about carrying out actions. The second line of the code above has the command print which means to display some text or numbers on the console. What is to be displayed is held between the brackets after the word print – in this case the phrase Hello World. The fact that the phrase is enclosed in quotation marks indicates that it is a piece of text.

What we have said about the second line also applies to the third line.

Now that we have explained the second and third lines we shall go back to the first one. This line begins with the hash character – ‘#’. What is this all about?

We stated above that a programme is a series of commands to the computer to perform certain tasks. We have looked above at once such command – print. Occasionally, however, we need put comments in among those commands, either as reminders to ourselves or as guides to others who may be subsequently reading the programme code. Of course we must also inform Python that those extra lines are comments and that they are to be ignored when the programme is running. The way we inform Python about this is to put the ‘#’ character as the first character of the line.

In Python programming it is common practice to put the name of the file that contains the programme code as a comment into the first line of the programme file. In order to make sure it is a comment we precede the file name with ‘#’, hence the first line is ‘#C1Example.py’.

Go to top

Running a Python script

To run a Python script, first ensure that the Python shell is running as shown in Figure 3. In the shell click on File/Open and navigate to the Python script you have just created and then open it.

This opens the script file as shown in Figure 4 below.

Examining Figure 4 we see that it is identical to the file we created. Here you can also edit the same file and once you save the changes, the saved version will be picked up by the text editor you used to create the file in the first place.

There are two ways to run the file:

  1. Click on Run/Run Module as shown in Figure 5
  2. Press F5

Whichever way you run the file, the window containing the file itself will go to the background and the shell will come to the foreground as shown in Figure 6

Here we see that our script has actually run and that the two messages that followed the print statements have actually appeared in the shell window.

If you are used to the zwizzy-zwazzy world Facebook and Google the output above may appear to be disappointing. Creating GUI interfaces is a lengthy process in Python and would be confusing to beginner programmers. Before creating them we need to look at other programming concepts such as variables, decisions, loops, functions, classes and objects. With those concepts under your belt you will then be in a position to tackle the topic of Graphical User Interface

Go to top

Summary

The installation files for Python can be downloaded from the site www.python.org/downloads/release/python-352/, and once downloaded can be installed on your local machine.

Python code is created using a text editor such as Notepad++, but never using a word processor.

Simplest way to run Python code is to open the Python script file using the shell and then run the file by pressing F5.

Go to top

Revision Questions

  1. Along with Python name another popular programming language.
  2. Where can you find the installation files for Python?
  3. Installing Python on your computer is very difficult. True or false?
  4. Once Python is installed on your computer how do you run it?
  5. What software do you use to create a Python script?
  6. Name one Python command and state what this command does.
  7. What is a comment line?
  8. How do we put comment lines into a Python script?
  9. What comment do we put into all Python scripts?
  10. How do you run a Python script?
Go to top