PYTHON: Introduction To Structured Programming
Writing Simple Programs
Introduction - What is Programming?
A program is a set of instructions that performs a set task and programming is the process of writing computer programs. For a computer to carry out our programs, they must be written in a programming language. In this unit we write our programs in Python. So if you haven't already, you may want to go install Python now (we shall be using Python 2.7+, but not Python 3).
Our First Program
Consider this problem:
Write a distance converter program that transforms a distance measured in miles into an equivalent distance in kilometres.
The first step in writing the program is to make sure that the problem is fully understood. This is achieved by writing a specification that states precisely what the program should do. This should then be agreed upon with whoever you are writing the program for before implementing the program. This specification should list all the program's inputs and outputs. For example, the specification of this distance converter program may include this:
- Input: A distance in miles.
- Output: A distance in kilometres, equivalent to the input distance.
The equation to relate miles into kilometres is:
kilometres = 1.609 X miles
This means that:
- 1 mile = 1.609 kilometres
- 10 miles = 16.09 kilometres
- 20 miles = 32.18 kilometres
It is useful to have these values before writing the program as we can use them to test that the program works once we have implemented it.
The next step in writing a program is to write an algorithm for it. Wiktionary defines an algorithm as:
A precise step-by-step plan for a computational procedure that begins with an input value and yields an output value in a finite number of steps.
Many developers write their algorithms in pseudo-code or plain English (or their native languages) before writing it out in code. Here is a possible algorithm for our program, written in plain English:
- Obtain a miles value from the user,
- Calculate a kilometres value, using kilometres = 1.609 X miles,
- Output the kilometres value.
This algorithm written in Python is as follows:
def milesToKilometres():
miles = input("Enter a distance in miles: ")
kilometres = 1.609 * miles
print "The distance in kilometres is", kilometres
Open up IDLE go to "File" then by "New Window" paste in this code (or type it out if you prefer) and save it as "milesToKilometres.py".
Testing the Program
Within the IDLE editor press F5 to run the program you have just written. To run the functions type out "milesToKilometres()" and press enter.
You can type out just the first few letters of the function name (e.g. "mile") and then press "alt" to auto complete the function name, so you don't have to write the whole name out.
The screen should display:
Enter a distance in miles:
Try inputting the values 1, 10 and 20 to see if they correspond with our predetermined test values. Like so:
Enter a distance in miles: 10 The distance in kilometres is 16.09
If you input anything other a number the program will produce an error. This is because the inputs haven't been sanitized and arithmetic operations can only be carried out on certain data types. I will explain this properly in another article.
If the programs gives us the correct answers we can assume it works. However, it is impossible to declare that a program works perfectly unless you have tested every single input, which would be impossible in this case as you could keep trying different inputs up to infinity. All we can truthfully declare is that it works in these cases.
Breaking the Program Down
Statements
Each line of the program is called a statement, each of the statements within the program are executed one after the other. The program will end after the last statement has been executed.
Variables
A variable denotes a location in computer memory where a value is stored. In Python, variables names are case-sensitive and their names may contain any letter or number, but they cannot start with a number and must not be identical to any of Python's keywords. Our distance converter program contains two variables: miles and kilometres.
Variables are by created by assignment statements, such as:
x = 12 + 7
First the expression on the right is executed giving the value 19. Secondly, the variable x is assigned this value of 19. If the variable x didn't already exist within the program, it would have been created. If the variable already existed, it's old value would have been replaced.
The "print" Statement
The print statement is used to dispaly information on the screen. The print statement can be used to display variables, or strings and these can be joined together using either ",", which will add a space between them or a "+" symbol, which will join them without adding a space. Here are a few examples:
print "Hello world!" # Output: Hello world! name = "Derek" print "Hello", name # Output: Hello Derek print "Hello " + name # Output: Hello Derek print "Hello " + name + ", how are you today?" # Output: Hello Derek, how are you today?
Comments
The text that followed each of those statements was a comment, comments follow the "#" character and are ignored by the computer. They are used to make code more readable and to explain what the program is doing, in case anyone ever needs to edit your code in the future.
Sources
- Poole, M. (2010). Writing Simple Programs. Retrieved November 20, 2010, from http://userweb.port.ac.uk/~poolem/python/slides01.pdf
- Lexical analysis — Python v2.7 documentation. Retrieved November 20, 2010, from http://docs.python.org/reference/lexical_analysis.html#keywords

