Professional Documents
Culture Documents
Global variables global currentUserID = 223 In Python, variables are global if they are currentUserID = 0
declared at the start of the program. If
you need to write to the variable in a def myProcedure():
function/procedure you must use the global currentUserID
‘global’ keyword before you can use it. currentUserID = 223
Input from name = input("Enter name") Outputs "Enter name" on screen. User name = input("Enter name")
keyboard types text. The text is stored as a string
inside the variable name
Iteration – Count for i = 0 to 9 Outputs "Hi" on the screen 10 times for i in range(0,10):
Controlled print("Hi") (from 0 to 9) – pseudocode includes the print("Hi")
(For loop) last number, Python doesn’t
Copyright © 2016 Axsied Ltd. | Axsied.com | Version 1.1 | OCR Pseudocode to Python 1
Syntax Topic OCR Pseudocode Result Python
Iteration – password = "" Asks the user for their Password? password = ""
while password != "CdRtp45@" while password != "CdRtp45@":
Condition password = input("Password?") Checks if the password is equal to password = input("Password?")
Controlled endwhile CdRtp45@
(While loop and Repeats if Password isn’t equal
Do-While loop)
Copyright © 2016 Axsied Ltd. | Axsied.com | Version 1.1 | OCR Pseudocode to Python 2
Syntax Topic OCR Pseudocode Result Python
Logical and Example: Loops if x is between 5 and 10 inclusive Example:
Comparison while x <= 10 AND x >= 5 AND is the logical operator in this while x <= 10 and x >= 5
Operators example
Arithmetic + Addition +
Operators - Subtraction -
* Multiplication *
/ Division /
MOD Modulus (remainder) %
DIV Quotient / Integer division / Floor division //
^ Exponentiation / Power of **
Copyright © 2016 Axsied Ltd. | Axsied.com | Version 1.1 | OCR Pseudocode to Python 3
Syntax Topic OCR Pseudocode Result Python
Selection if choice == "heads" then if choice == "heads":
print("You chose heads") print("You chose heads")
elseif choice == "tails" then elif choice == "tails":
print("You chose tails") print("You chose tails")
else else:
print("Invalid choice") print("Invalid choice")
endif
Example:
myName = "Sophie" myName = "Sophie"
print(myName.length) Outputs 6 on the screen print(len(myName))
Substring stringname.subString(startingPosition stringname[start:end]
,numberOfCharacters)
Example:
myName = "Sophie" Outputs ph on the screen myName = "Sophie"
print(myName.substring(2,2)) print(myName[2:4])
Note that the character position starts at
the 0th character which is S in this
example
Copyright © 2016 Axsied Ltd. | Axsied.com | Version 1.1 | OCR Pseudocode to Python 4
Syntax Topic OCR Pseudocode Result Python
Subroutines - function add(firstNum, secondNum) This function takes two arguments def add(firstNum, secondNum):
total = firstNum + secondNum total = firstNum + secondNum
Functions return total (inputs) which are firstNum and return total
endfunction secondNum and returns the total of the
two added together
totalScore = add(5,3) totalScore = add(5,3)
showNames("Sam", "Green")
(inputs) which are firstName and
lastName. When the procedure is called
the names are concatenated together
and output to the screen
Pass by a = 0 Output: Python doesn’t work in the same way as
b = 0
Reference 0 the pseudocode and there is no option to
Pass by Value procedure move(x: byVal, y: byRef) 1 pass by reference. For most cases you can
x = x + 1 think of Python as passing by value.
y = y + 1
endprocedure
In the pseudocode, the variable x will be
passed to the procedure by copying the
move(a, b) value. Changing x in the procedure won’t
print(a)
print(b)
change the original value.
Copyright © 2016 Axsied Ltd. | Axsied.com | Version 1.1 | OCR Pseudocode to Python 5
Syntax Topic OCR Pseudocode Result Python
Arrays array testScores[4] Outputs testScores = [90, 100, 50, 75]
testScores[0] = 90
testScores[1] = 100 50 print(testScores[2])
testScores[2] = 50
testScores[3] = 75 Array referencing starts at 0, so to access
print(testScores[2])
the first element in the array, use: Alternatively:
testScores[0] testScores = [None]*4
testScores[0] = 90
testScores[1] = 100
Python uses lists not arrays. These are testScores[2] = 50
different to array as mixed data types can testScores[3] = 75
be stored inside them – for example, you
can store integers and strings inside the print(testScores[2])
same list – in an array you would need to
use two lists, one for each data type
Copyright © 2016 Axsied Ltd. | Axsied.com | Version 1.1 | OCR Pseudocode to Python 6
Syntax Topic OCR Pseudocode Result Python
2D Arrays array board[8, 8] The first line of code creates a 2D array – board = [[None]*8 for _ in range(8)]
board[0,0] = "castle" board[0][0] = "castle"
this is an array that contains other arrays;
think of it as a grid or table.
#alternative way to build the array
board = []
You would expect that the following for i in range(8):
Python code would initialise the array: row = []
board = [[None]*8]*8 for j in range(8):
row.append(None)
This will not work as each row in the board.append(row)
array will be a reference to the first one.
So when you change board[0][0] you board[0][0] = "castle"
would change the entire first column.
Copyright © 2016 Axsied Ltd. | Axsied.com | Version 1.1 | OCR Pseudocode to Python 7
Syntax Topic OCR Pseudocode Result Python
Reading to the myFile = openRead("example.txt") To read all the lines in a file, first open myFile = open("example.txt", 'r')
End of File while NOT myFile.endOfFile() the file, then use a loop to read each of for line in myFile:
print(myFile.readLine()) the lines, then close the file. print(line)
endwhile
myFile.close()
myFile.close()
In pseudocode (and many other
languages) we read the line until we
reach end of file (EOF). In Python we use #remove the new line character when
#reading from a file
a for loop to do this. myFile = open("example.txt")
Copyright © 2016 Axsied Ltd. | Axsied.com | Version 1.1 | OCR Pseudocode to Python 8