Turn over
W75838A
©2024 Pearson Education Ltd. 1/1/1
Contents
Introduction 4
Comments 5
Identifiers 5
Data types and conversion 5
Primitive data types 5
Conversion 5
Constants 5
Combining declaration and initialisation 5
Structured data types 5
Dimensions 5
Operators 6
Arithmetic operators 6
Relational operators 6
Logical/Boolean operators 6
Programming constructs 7
Assignment 7
Sequence 7
Blocking 7
Selection 7
Repetition 7
Iteration 7
Subprograms 8
Inputs and outputs 8
Screen and keyboard 8
Files 8
Supported subprograms 9
Built-in subprograms 9
List subprograms 10
String subprograms 11
Formatting strings 12
Library modules 13
Random library module 13
Math library module 13
Time library module 13
Turtle graphics library module 14
Tips for using turtle 14
Turtle window and drawing canvas 14
Turtle creation, visibility and movement 15
Turtle positioning and direction 15
Turtle filling shapes 15
Turtle controlling the pen 16
Turtle circles 16
Turtle colours 16
Console session 16
Code style 16
Line continuation 17
Carriage return and line feed 17
Introduction
The Programming Language Subset (PLS) is a document that specifies which parts of Python 3 are required in order that the assessments can be undertaken with confidence. Students familiar with everything in this document will be able to access all parts of the Paper 2 assessment. This does not stop a teacher/student from going beyond the scope of the PLS into techniques and approaches that they may consider to be more efficient or engaging.
Pearson will not go beyond the scope of the PLS when setting assessment tasks. Any student successfully using more esoteric or complex constructs or approaches not included in this document will still be awarded marks in Paper 2 if the solution is valid.
The pair of symbols indicates where expressions or values need to be supplied. They are not part of the PLS.
Comments
Anything on a line after the character # is considered a comment.
Identifiers
Identifiers are any sequence of letters, digits and underscores, starting with a letter. Both upper and lower case are supported.
Data types and conversion Primitive data types
Variables may be explicitly assigned a data type during declaration. Variables may be implicitly assigned a data type during initialisation. Supported data types are:
Data type PLS
integer int
real float
Boolean bool
character str
Conversion
Conversion is used to transform the data types of the contents of a variable using int(), str(), float(), bool() or list(). Conversion between any allowable types is permitted.
Constants
Constants are conventionally named in all uppercase characters.
Combining declaration and initialisation
The data type of a variable is implied when a variable is assigned a value.
Structured data types
A structured data type is a sequence of items, which themselves are typed. Sequences start with an index of zero.
Data type Explanation PLS
string A sequence of characters str
array A sequence of items with the same (homogeneous) data type list
record A sequence of items, usually of mixed (heterogenous) data types list
Dimensions
The number of dimensions supported by the PLS is two.
The PLS does not support ragged data structures. Therefore, in a list of records, each record will have the same number of fields.
Operators Arithmetic operators
Arithmetic operator Meaning
/ division
* multiplication
** exponentiation
+ addition
– subtraction
// integer division
% modulus
Relational operators
Relational operator Meaning
== equal to
!= not equal to
greater than
= greater than or equal to
less than
= less than or equal to
Logical/Boolean operators
Operator Meaning
and both sides of the test must be true to return true
or either side of the test must be true to return true
not inverts
Programming constructs Assignment
Assignment is used to set or change the value of a variable.
Sequence
Every instruction comes one after the other, from the top of the file to the bottom of the file.
Blocking
Blocking of code segments is indicated by indentation and subprogram calls. These determine the scope and extent of variables they declare.
Selection
if expression:
command If expression is true, then command is executed.
if expression: If expression is true, then first command is
command executed, otherwise second command is executed.
else:
command
if expression:
command elif expression:
command If expression is true, then first command is executed, otherwise the second expression test is checked. If true, then second command is executed, otherwise third command is executed.
else:
command Supports multiple instances of ‘elif’.
The ‘else’ is optional with the ‘elif’.
Repetition
Iteration
for id in structure:
command Executes command for each element of a data structure, in one dimension.
for id in range (start, stop):
command Count-controlled loop. Executes command a fixed number of times, based on the numbers generated by the range function. stop is required. start is optional.
for id in range (start, stop,
step):
command Same as above, except that step influences the numbers generated by the range function.
stop is required. start and step are optional.
Subprograms
def procname ():
command A procedure with no parameters
def procname (paramA, paramB):
command A procedure with parameters
def funcname ():
command
return (value) A function with no parameters
def funcname (paramA, paramB):
command
return (value) A function with parameters
Inputs and outputs Screen and keyboard
print (item) Displays item on the screen
input (prompt) Displays prompt on the screen and returns the line typed in
Files
The PLS supports manipulation of comma separated value text files. File operations include open, close, read, write and append.
fileid = open (filename, "r") Opens file for reading
for line in fileid: Reads every line, one at a time
alist = fileid.readlines () Returns a list where each item is a line from the file
aline = fileid.readline () Returns a line from a file. Returns an empty string on the end of the file
fileid = open (filename, "w") Opens a file for writing
fileid = open (filename, "a") Opens a file for appending
fileid.writelines (structure) Writes structure to a file. structure is a list of strings
fileid.write (aString) Writes a single string to a file
fileid.close () Closes file
Supported subprograms Built-in subprograms
The PLS supports these built-in subprograms.
Subprogram Description
bool (item) Returns item converted to the equivalent Boolean value
chr (integer) Returns the string which matches the Unicode value of integer. The first 128 characters of Unicode are equivalent to ASCII.
float (item) Returns item converted to the equivalent real value
input (prompt) Displays the content of prompt to the screen and waits for the user to type in characters followed by a new line
int (item) Returns item converted to the equivalent integer value
len (object) Returns the length of the object, such as a string, one-dimensional or two-dimensional data structure
ord (char) Returns the integer equivalent to the Unicode string of the single character char. The first 128 characters of Unicode are equivalent to ASCII.
print (item) Prints item to the display
range (start, stop, step) Generates a list of numbers using step, beginning with start and up to, but not including, stop. A negative value for step goes backwards. stop is required. start and step are optional. The default value for
start is zero. The default value for step is positive one.
round (x, n) Rounds x to the number of n digits after the decimal (uses the 0.5 rule). The n is optional. If omitted, the function returns the nearest integer to x.
str (item) Returns item converted to the equivalent string value
List subprograms
The PLS supports these list subprograms.
Subprogram Description
list.append (item) Adds item to the end of the list
del list [index] Removes the item at index from list
list.insert (index, item) Inserts item just before an existing one at index
aList = list ()
aList = [] Two methods of creating a list structure. Both are empty.
String subprograms
The PLS supports these string subprograms.
Subprogram Description
len (string) Returns the length of string
string.find (substring,
start, end) Returns the location of the first instance of
substring in the original string, reading from left to right. start is the index to begin the find. The default is zero. end is the index to stop the find.
The default is the end of the string. Returns -1, if not found.
string.index (substring,
start, end) Returns the location of the first instance of
substring found in the original string as read from left to right. Raises an exception if not found.
substring is required. start and end are optional. The default value for start is zero.
The default value for end is the end of the string.
string.isalpha () Returns True, if all characters are alphabetic A–Z
string.isalnum () Returns True, if all characters are alphabetic A–Z or digits 0–9
string.isdigit () Returns True, if all characters are digits 0–9, exponents are digits
string.replace (s1, s2) Returns original string with all occurrences of s1 replaced with s2
string.split (char) Returns a list of all substrings in the original, using
char as the separator
string.strip (char) Returns original string with all occurrences of char removed from the front and back
string.upper () Returns the original string in uppercase
string.lower () Returns the original string in lowercase
string.isupper () Returns True, if all characters are uppercase
string.islower () Returns True, if all characters are lowercase
string.format (placeholders) Formats values and puts them into the
placeholders
Formatting strings
Output can be customised to suit the problem requirements and the user’s needs by forming string output.
string.format () can be used with positional placeholders and format descriptors. Placeholders take the form:
{:alignsignwidth.precisiontype}
Placeholder Option Description
align Left aligned. Default for most items, like text.
Right aligned. Default for numbers.
^ Centre aligned.
sign + Use a sign for both positive and negative numbers.
− Use a sign only for negative numbers. Default for negative numbers.
space Use leading spaces for positive numbers and a minus sign for negative numbers.
width whole number The total width of the field.
precision whole number The number of digits after the decimal.
type s String. Default for strings, if not supplied.
d Numbers in base 10 (denary). Default for integers, if not supplied.
f Fixed-point notation. Formats a number with exactly the number of digits to the right of the decimal given by precision
Here is an example:
layout = "{:10} {:^5d} {:7.4f}"
print (t (“Fred”, 358, 3.14159))
The * operator can be used to generate a line of repeated characters, for example: “=” * 10 will generate “==========”.
Concatenation of strings is done using the + operator.
String slicing is supported. myName[0:2] gives the first two characters in the variable
Content preview
Computer science
Pearson Edexcel Level 1/Level 2
GCSE (9–1)
1CP2/
Paper
referen
ce
Computer Science 02
PAPER 2: Application of Computational
Thinking Programming Language Subset
Version 5
PLS Booklet
You do not need any other materials.
Turn over
Computer science
,W75838A
©2024 Pearson Education
Ltd. 1/1/1
, Contents
Introduction.................................................................................................................4
Comments...................................................................................................................5
Identifiers....................................................................................................................5
Data types and conversion.........................................................................................5
Primitive data types...............................................................................................5
Conversion.............................................................................................................5
Constants...............................................................................................................5
Combining declaration and initialisation................................................................5
Structured data types............................................................................................5
Dimensions............................................................................................................5
Operators....................................................................................................................6
Arithmetic operators..............................................................................................6
Relational operators...............................................................................................6
Logical/Boolean operators......................................................................................6
Programming constructs.............................................................................................7
Assignment............................................................................................................7
Sequence...............................................................................................................7
Blocking.................................................................................................................7
Selection................................................................................................................7
Repetition...............................................................................................................7
Iteration..................................................................................................................7
Subprograms..........................................................................................................8
Inputs and outputs......................................................................................................8
Screen and keyboard.............................................................................................8
Files........................................................................................................................8
Supported subprograms.............................................................................................9
Built-in subprograms..............................................................................................9
List subprograms..................................................................................................10
String subprograms..............................................................................................11
Formatting strings...........................................................................................12
2 W7583
8A