登录

Variables, constants and data types

Data Types

The following keywords are used to designate some basic data types:

Types

Explaination

INTEGER

a whole number

REAL

a number capable of containing a fractional part

CHAR

a single character

STRING

a sequence of zero or more characters

BOOLEAN

the logical values TRUEand FALSE

DATE

a valid calendar date

Literals

Literals of the above data types are written as follows:

Types

Explaination

Integer

Written as normal in the denary system, e.g. 5, -3

Real

Always written with at least one digit on either side of the decimal point, zeros being added if necessary, e.g. 4.7, 0.3, -4.0, 0.0

Char

A single character delimited by single quotes e.g. ꞌxꞌ, ꞌCꞌ, ꞌ@ꞌ

String

Delimited by double quotes. A string may contain no characters (i.e. the empty string) e.g. "This is a string", ""

Boolean

TRUE, FALSE

Date

This will normally be written in the format dd/mm/yyyy. However, it is good practice to state explicitly that this value is of data type DATE and to explain the format (as the convention for representing dates varies across the world).

The following basic data types are commonly used in Python (other programming languages may distinguish between single characters and strings) :

Data type

Description

Example

int

Integer

num=1

float

Fraction

num=1.5

str

Single Character

character='a'

str

Characters

word='hello'

bool

True or False

judge=True

score = 100 #  int
height = 1.85 #  float
name = "Oldmoon" #  str
is_pass = True # bool

Identifiers

Identifiers (the names given to variables, constants, procedures and functions) are in mixed case. They can only contain letters (A–Z, a–z), digits (0–9) and the underscore character ( _ ).

They must start with a letter and not a digit. Accented letters should not beused.

It is good practice to use identifier names that describe the variable, procedure or function they refer to. Single letters may be used where these are conventional (such as i and j when dealing with array indices, or X and Y when dealing with coordinates) as these are made clear by the convention.

Keywords identified elsewhere in this guide should never be used as variables.

Identifiers should be considered case insensitive, for example, Countdown and CountDown should not be used as separate variables.

Variable declarations

It is good practice to declare variables explicitly in pseudocode.

Declarations are made as follows:

DECLARE <identifier> : <data type>
//Example – variable declarations
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN

Constants

It is good practice to use constants if this makes the pseudocode more readable, as an identifier is more meaningful in many cases than a literal. It also makes the pseudocode easier to update if the value of the constant changes.

Constants are normally declared at the beginning of a piece of pseudocode (unless it is desirable to restrict the scope of the constant).

Constants are declared by stating the identifier and the literal value in the following format:

CONSTANT <identifier> = <value>
//Example – CONSTANT declarations
CONSTANT HourlyRate = 6.50
CONSTANT DefaultText = "N/A"

Only literals can be used as the value of a constant. A variable, another constant or an expression must never be used.

Assignments

The assignment operator is ← .

Assignments should be made in the following format:

<identifier> ← <value>

The identifier must refer to a variable (this can be an individual element in a data structure such as an array or a user defined data type). The value may be any expression that evaluates to a value of the same data type as the variable.

//Example – assignments
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate

Python variables do not need to be explicitly declared to preserve memory space. The declaration occurs automatically when you assign a value to a variable. The equal sign = is used to assign a value to a variable.

The operand to the left of the = operator is the name of the variable, and the operand to the right of the = operator is the value stored in the variable.

myInt = 4 means that the value 4 is assigned to the variable named myInt.

myInt = 4
myReal = 2.5
myChar = 'a'
myString = 'hello'
print(myInt)
print(myReal)
print(myChar)
print(myString)

In the above code, myInt, myReal, myChar, and myString are variable names, while 4/2.5, 'a', and 'hello' are variable values.

Variables can also reference and operate on each other, run the following code to try it out:

a = 1
b = 1
a = b + b
print(a)
print(b)

登录