Programming and data representation
Programming basics:
All that is written here is in pseudocode. The python is in this file:
Declaration of variables:
DECALRE <identifier> : <datatype>
Declaration and assignments of constants:
CONSTANT <identifier> = <value>
Assignment of variables:
<identifier> <- <expression>
Arithmetic operators:
Outputting information to the screen:
OUTPUT <string>
OUTPUT <identifier>
Getting input from the user:
INPUT “Enter a number:” A
Data types:
Date is not built into python and java
Boolean expressions:
Comparison operators:
Boolean operators:
Selection:
IF…THEN statements:
IF <Boolean expression>
THEN
<statement>
ENDIF
IF…THEN…ELSE statements:
IF <Boolean expression>
THEN
<statement>
ELSE
<statement>
ENDIF
Nested IF statements:
IF <Boolean expression>
THEN
<statements>
ELSE
IF <Boolean expression>
THEN
<statement>
ELSE
<statement>
ENDIF
ENDIF
Case statements:
CASE OF <expression>
<value1> : <statement>
<value2>, <value3> : <statement>
<value4> TO <value5> : <statement>
OTHERWISE <statement>
ENDCASE
Eg.
Iteration:
Count-controlled (FOR) loops - In pseudocode, count-controlled loop is written as:
The control variable starts with value s, increments by value I each time round the loop and
finished when the control variable reaches the value e
In python a count-controlled loop is written as:
for <control variable> in range (s, e, i) :
<statement>
Post-conditional loop – A post-conditional loop makes sure that the loop runs at least once.
In pseudocode a post-conditional loop is written as:
There is no post-conditional loop option in python
Pre-conditional loop – A pre-conditional loop evaluates the condition before the statements
within the loop are executed. In pseudocode a pre-conditional loop is written as:
In python a pre-conditional loop is written as:
while <condition> :
<statement(s)>
Built in functions:
String manipulation functions:
Truncating numbers – Instead of rounding you may just want the whole number part of a
number:
Converting a string to a number – If you want to convert a string to a number in python for
example the string “5” stored in the variable S:
int (S)
If there is a decimal in the number that is stored int the string then all you do is:
float (S)
In pseudocode this is how you do it:
STRING_TO_NUMBER (x: STRING) RETURNS REAL
Random number generator:
randint (1, 6)
Procedures and functions:
You have already learned about procedures and functions but this is how you create them:
def <identifier> () :
I
<statements>
In order to change this into a function all you must do is return a value:
def <identifier> () :
I
<statements>
return <value>
Return value – The value replacing the function call used in the expression
Passing parameters to subroutines:
In the brackets you put a variable that is then passed to the subroutine. When calling the
subroutine you can put in a value that will then be passed into the subroutine as the
variable.
Arrays:
Declaring a 1D array is pseudocode:
DECLARE <arrayIdentifier>: ARRAY [<lowerBound>: <upper Bound>] OF <datatype>
Python examples:
Accessing 1D arrays in pseudocode and python:
<ArrayIdentifier>[x]
Creating 2D arrays in pseudocode:
DECLARE <arrayIdentifier>: ARRAY [<lowerBound>: <upper Bound>], [<lowerBound>:
<upper Bound>] OF <datatype>
Python examples:
Accessing 2D arrays in pseudocode:
<ArrayIdentifier>[x, y]
Accessing 2D arrays in python:
<ArrayIdentifier> [2] [3] = 0 Elements are numbered from 0 in python so this gives access
to the 4th element in the 3rd element
Text files:
Writing to a text file:
Reading from a text file:
Appending to a text file:
The end-of-file (EOF) marker: