← Cambridge IGCSE

Cambridge pseudocode and Python side by side

Comment

// This is a comment
# This is a comment

Declare a variable

DECLARE Count : INTEGER
count = 0  # Python has no declarations

Data types

INTEGER, REAL, CHAR, STRING, BOOLEAN
int, float, str (one character), str, bool

Literals

5   4.7   'x'   "Hello"   TRUE
5   4.7   "x"   "Hello"   True

Declare a constant (literal values only)

CONSTANT HourlyRate ← 6.50
HOURLY_RATE = 6.50

Assignment

Total ← Total + 1
total = total + 1

Input

INPUT Answer
answer = input()

Output (several values)

OUTPUT "You have ", Lives, " lives left"
print("You have " + str(lives) + " lives left")

Arithmetic

+  -  *  /  ^ (power)
+  -  *  /  **

Integer division and remainder

DIV(10, 3)  MOD(10, 3)
10 // 3   10 % 3

Relational operators

=  <>  <  <=  >  >=
==  !=  <  <=  >  >=

Logical operators

AND  OR  NOT
and  or  not

IF with ELSE

IF Mark >= 50
  THEN
    OUTPUT "Pass"
  ELSE
    OUTPUT "Fail"
ENDIF
if mark >= 50:
    print("Pass")
else:
    print("Fail")

CASE statement

CASE OF Move
  'L' : X ← X - 1
  'R' : X ← X + 1
  OTHERWISE OUTPUT "Beep"
ENDCASE
if move == "L":
    x = x - 1
elif move == "R":
    x = x + 1
else:
    print("Beep")

Count-controlled loop

FOR Count ← 1 TO 10
    OUTPUT Count
NEXT Count
for count in range(1, 11):
    print(count)

FOR loop with STEP

FOR Count ← 10 TO 0 STEP -2
    OUTPUT Count
NEXT Count
for count in range(10, -1, -2):
    print(count)

Pre-condition loop

WHILE Number > 9 DO
    Number ← Number - 9
ENDWHILE
while number > 9:
    number = number - 9

Post-condition loop (runs at least once)

REPEAT
    INPUT Password
UNTIL Password = "Secret"
while True:
    password = input()
    if password == "Secret":
        break

1D array

DECLARE Names : ARRAY[1:30] OF STRING
Names[1] ← "Ali"
names = [""] * 30
names[0] = "Ali"  # Python lists start at 0

2D array

DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
Board[2, 3] ← 'X'
board = [[""] * 3 for _ in range(3)]
board[1][2] = "X"

String length and case

LENGTH(Name)  UCASE(Name)  LCASE(Name)
len(name)  name.upper()  name.lower()

Substring (start position, length)

SUBSTRING("Happy Days", 1, 5)
"Happy Days"[0:5]

Rounding and random numbers

ROUND(Price, 2)
RANDOM()  // between 0 and 1
round(price, 2)
import random
random.random()

Procedure with parameters

PROCEDURE Line(Size : INTEGER)
    OUTPUT Size
ENDPROCEDURE
CALL Line(60)
def line(size):
    print(size)

line(60)

Function

FUNCTION Square(N : INTEGER) RETURNS INTEGER
    RETURN N * N
ENDFUNCTION
OUTPUT Square(4)
def square(n):
    return n * n

print(square(4))

Local variable inside a subroutine

PROCEDURE Show
    DECLARE Temp : INTEGER
    Temp ← 5
ENDPROCEDURE
def show():
    temp = 5  # local to show()

Read a line from a file

OPENFILE "Data.txt" FOR READ
READFILE "Data.txt", LineOfText
CLOSEFILE "Data.txt"
f = open("Data.txt", "r")
line_of_text = f.readline().strip()
f.close()

Write a line to a file (WRITE replaces old contents)

OPENFILE "Data.txt" FOR WRITE
WRITEFILE "Data.txt", LineOfText
CLOSEFILE "Data.txt"
f = open("Data.txt", "w")
f.write(line_of_text + "\n")
f.close()