Scientific computing with Python

by Conor Lawless email: conor.lawless@ncl.ac.uk

Writing your first Python script

A tradition, when learning a new programming language, is to use it to make your computer print "Hello World!" to screen. To achieve this, you need to overcome two hurdles: a) write a simple command in a way that the Python interpreter can understand (command with correct Python syntax) and b) execute the command.

Script format

It's important to note that a Python script or progam is simply a text file with a .py extension. An easy way to create a script is to open a text editor (e.g. Notepad on Windows), write some Python commands, save the file as "myscript.py", for example. Note that although this file is a text file, the extension must be ".py" to signify that the file's contents are written in the Python language.

Creating and saving Python scripts under MS Windows

I strongly recommend downloading and using the free Notepad++ text editor for use instead of Notepad on Windows machines. Remember that you will need administrator rights to install software.

Be aware that default Windows settings hide the extensions of files, making it difficult to verify that you have set extensions correctly. However, highlighting a file and pressing the F2 button (to allow renaming a file) should always reveal the full filename, including extensions. I recommend forcing Windows to always display extensions, if you can.

Unless you specify otherwise, Microsoft text editors like Notepad will assume ".txt" as a default extension. Some versions of Windows will append ".txt" to your filename even if you manually specify an extension (e.g. "myscript.py" becomes "myscript.py.txt")! To force the correct behaviour, in Notepad's "Save As..." dialogue box you can include inverted commas around the filename and extension, or choose "*.*" from the dropdown menu for file type.

An example script

Let's create a Python script by copying the two coloured lines of text below into an empty text file, saving the file as "myscript.py".

# First script!
print("Hello World!")

There are several different ways to run this script, depending on what's most convenient and what's available on your machine. We will learn about some of these on the next page.

Before we do that, let's look at an alternative text editor, specifically designed for writing and examining Python code: the one which comes with the IDLE Python interpreter.

On your own Windows machine, you can start the IDLE Python interpreter as follows:

Start -> All Programs -> Python 3.5 -> IDLE (Python GUI)

However, Newcastle's IT department have installed Python in a very unusual location on machines in the Dene cluster, so if you are using one of those machines:

Start -> All Programs -> ARCGIS -> Python 2.7 -> IDLE (Python GUI)

The IDLE Python interface provides a live console (or shell) for executing Python commands. It also includes a useful text editor, specifically designed to help with writing, understanding and running Python code. From the menu above the IDLE console, choose File -> New Window to start writing a new script. If you copy and paste the text above into this Untitled window, and save the file as "myscript.py" you will see that the editor automatically colours the text. The IDLE text editor colours text depending on whether it contains comments, strings etc. and later we will see that it helps with tasks like indenting text and ensuring sets of brackets are closed.

Save this script before we execute it. File -> Save As -> myscript.py


OverviewInstallationFirst ScriptExecutionLibrariesStructureOther Resources


Last updated: April 2016