Python
Python
Computation
kɒmpjʊˈteɪʃ(ə)n/
noun
Computation is any type of calculation that includes both arithmetical and logical steps and
follows a well-defined model.
Programming
Translators
Compiler
What is a program?
Programs are made up of statements that the programming language knows and
understands.
Just as words are put together to form a sentence, a program puts one or more statements
together to form an instruction. Each statement tells the computer to perform a specific
task, and the instructions tell a computer what to do.
Statements
Different programming languages use different statements. A few of these are listed in this
table:
Statement Purpose
print Output a message on the screen
input Get data from the user
if…else A decision
while A loop controlled by a decision
for A loop controlled by a counter
def Create a procedure or function
Consider this simple problem. A cinema is offering discount tickets to anyone who is under
15. Decomposing this problem, gives this algorithm:
2. if the person is younger than 15 then say “You are eligible for a discount ticket.”
To convert the flowchart or pseudocode into a program, look at each individual step, and
write an equivalent instruction.
Basic arithmetic
Variables
All variables are made up of three parts: name, type and value.
Spacing: variable names should not have a space in them. Use underscores or
camelCase instead, eg total_money; totalMoney).
Data types
Variables come in all shapes and sizes. Some are used to store numbers,
some are used to store text and some are used for much more
complicated types of data.
A variable can hold a number, but it can also hold a piece of text. Just one letter is called a
character. More than one character is called a string.
A text variable works in the same way as a number variable, with a couple of differences:
IF statements
1. Ask how old you are
The selection comes in step 2. If you are aged 70 or over, one message is displayed.
As a flowchart:
IF...ELSE statements
In programming, selection is usually represented by the statements IF and ELSE:
IF…ELSE
Ask how old you are
ELIF statements
This simple algorithm prints out a different message depending on how old you are. Using
IF, ELSE and ELSE IF, the steps are:
• ELSE IF you are exactly 50, say “Wow, you are half a century old!”
Sorting Data
There are many types of sorting algorithms. Here is an example of one such algorithm,
bubble sort.
Bubble sort
A bubble sort algorithm goes through a list of data a number of times, comparing two items
that are side by side to see which is out of order. It will keep going through the list of data
until all the data is sorted into order. Each time the algorithm goes through the list it is
called a ‘pass’.
Example
Imagine that you have a list of people who you want to sort by age, from youngest to
oldest. A bubble sort can do this.
First pass
The highlighted numbers are the numbers that are being compared.
The first two numbers are compared. 15 is smaller than 41 so they switch places.
The next two numbers are compared. 17 is smaller than 41 so they switch places.
The next two numbers are compared. 32 is smaller than 41 so they switch places.
The next two numbers are compared. 18 is smaller than 41 so they switch places.
The next two numbers are compared. 28 is smaller than 41 so they switch places.
The next two numbers are compared. 41 is smaller than 77 so no change occurs.
The next two numbers are compared. 54 is smaller than 77 so they switch places.
This is what the list looks like after the first pass.
The list is now more ordered than it was originally, but it isn’t yet fully in order of youngest
to oldest. The list needs to go through another pass to compare the numbers again, so it
can be sorted further.
Second pass
This is what the list looks like after the first pass.
The first two numbers are compared. 15 is smaller than 17 so no change occurs.
15, 17, 32, 18, 28, 41, 54, 77
The next two numbers are compared. 17 is smaller than 32 so no change occurs.
The next two numbers are compared. 18 is smaller than 32 so they switch places.
The next two numbers are compared. 28 is smaller than 32 so they switch places.
The next two numbers are compared. 32 is smaller than 41 so no change occurs.
The next two numbers are compared. 41 is smaller than 54 so no change occurs.
The next two numbers are compared. 54 is smaller than 77 so no change occurs.
This is what the list looks like after the second pass.
The set of data is now in order from youngest to oldest, but the algorithm does not know
this yet as it made some changes in the second pass. The algorithm will only recognise
that the list is in order if it makes no changes in a pass. The algorithm therefore needs to
run for a third pass to compare the numbers again. As no changes will be made, the
algorithm will then recognise that the data is in order.
If the data being sorted is a large set of data, it may take several passes to get the data
sorted.
One of the main advantages of a bubble sort is that it is a very simple algorithm to
describe to a computer. There is only really one task to perform (compare two values and,
if needed, swap them). This makes for a very small and simple computer program.
The biggest problem with a bubble sort is that it takes a very long time to run. For
example, if there are 100 values to sort, each pass through the list will take 99
comparisons – and you might have to repeat it 99 times.