Basic Programming Syntax (Python)
Basic Programming Syntax (Python)
Contents
1 Comments 3
2 Assignment 3
3 Data Types 4
4 Functions 5
4.1 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.2 Other Library Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5 Operations 7
5.1 Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.2 Logical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.3 String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7 Selections 11
7.1 IF statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7.2 CASE statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
8 Arrays 13
8.1 1D Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
8.2 2D Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
9 File Handling 14
2
Comments
Comments in code act as a form of note that programmers might add to explain sections of programs
or other forms of logic. They are ignored by the compiler when the program is being executed. These
comments help make the code maintainable and manageable both for the program author and other
programmers.
Comments can be generalized into three forms, these are as follows:
• Multi-line comments
• Single-line comments
• Inline comments
In Python, multi-line comments are delimited by ''' and, single-line and inline comments are
preceded with #.
Python
Multi-line comments
'''
This is an example of a multi-line comment
It is normally used for larger explanations
'''
Single-line comments
# This is an example of a single-line comment
Inline comments
print(result) # This is an example of an inline comment
Assignment
In Python, the assignment operator is =
Assignment statements are written in the following format:
<identifier> = <value>
Python
3
Data Types
In programming, there are a few keywords used to designate certain data types, these are as follows:
int
float
str
bool
Literals
Literals of these data types are written as follows:
4
Functions
In programming there are a few built-in library functions that may be provided.
Python
Input function
answer = input()
Output function
print(score)
5
Other Library Functions
In Python these library functions can be written as:
round(<identifier>, <places>)
Returns the value of the identifier rounded to places number of decimal places. The identifier
should be any value that evaluates to the REAL data type whilst the number of places should be a
positive integer.
random()
Returns a random decimal number between 0 and 1 inclusive.
Python
Rounding function
round(3.1415, 1)
Returns 3.1
Random function
random() * 5
Returns a random decimal number between 0 and 5 inclusive
round(random() * 10), 0)
Returns a random integer between 0 and 10 inclusive
6
Operations
In programming, there are three types of operations, these are as follows:
• Arithmetic operations
• Logical operations
• String operations
Arithmetic Operations
For arithmetic operations, standard mathematical operator symbols are used. These operator
symbols are as follows:
Python
Addition +
Subtraction -
Multiplication *
Division /
Raised to the power of **
Python
Python
7
Logical Operations
The following symbols are used for logical operations:
Python
Equal to ==
Less than <
Less than or equal to <=
Greater than >
Greater than or equal to >=
Not equal to !=
These operations evaluate expressions to bool data type values.
String Operations
String operations are used to manipulate values of the STRING data type.
In Python, the basic string operations are written as follows:
len(<identifier>)
Returns the integer value representing the length of the string.
(<identifier>).lower()
Returns the string/character with all characters in lower case.
(<identifier>).upper()
Returns the string/character with all characters in upper case.
<identifier>[<start>:<end>]
Returns a string that starts from the position start and ends at the position end exclusive
Python
8
Iterations and Loops
In programming, there are three types of iterations. These are as follows:
• Count-controlled FOR loop
• Post-conditional REPEAT loop
• Pre-conditional WHILE loop
Python
Count-controlled loop
for index in range(30):
print(numbers[index])
9
Pre-conditional WHILE Loop
In pre-conditional loop, the condition must be an expression that evaluates to an bool data type.
The condition is tested before the statements are executed, and the statements will only be executed
if the condition evaluates to True. After the conditions have been executed the conditions are tested
again. The loop terminates when the condition evaluates to False.
In Python, pre-conditional loops are written as follows:
while <conditions>:
<statements>
Python
Pre-conditional loop
while number < 10:
print(number)
number = number + 1
10
Selections
There are two functions of selections this booklet practices, these are as follows:
• IF statements
• CASE statements
IF statements
In Python, an IF statement without any clauses is written as follows:
if <condition>:
<statements>
An IF statement with an elif clause is written as follows:
if <condition>:
<statements>
elif <condition>:
<statements>
There can be multiple elif clauses in a IF selection statement. Please note that elif clauses do
not completely align with the syllabus, candidates should use it with caution.
An IF statement with an ELSE clause is written as follows:
if <condition>:
<statements>
else:
<statements>
Python
11
CASE statements
In Python, CASE statements are written as follows:
match <identifier>:
case <value n>:
<statements>
An OTHERWISE clause (indicated by _) can be added as follows:
match <identifiers>:
case <value n>:
<statements>
case _:
<statements>
Python
12
Arrays
Arrays are fixed-length structures of elements that have identical data type, accessible by consecutive
index numbers. Square brackets, [<index>] are used to indicate the array indices.
In programming, arrays can have multiple dimensions, however this booklet only practices 1D and
2D arrays.
1D Arrays
In Python, 1D arrays are initialized as follows:
<identifier> = [None] * <u>
<identifier> = [0] * <u>
Where u stands for upper bound.
1D arrays are assigned in the following way:
<identifier>[<index>] = <value>
Python
1D array initialization
student_marks = [0] * 30
1D array assignment
student_marks[15] = 88
2D Arrays
In Python, 2D arrays are initialized as follows:
<identifier> = [[None] * <ur>] * <uc>
<identifier> = [[0] * <ur>] * <uc>
Where r stands for row, c stands for column and u stands for upper bound.
2D arrays are assigned as follows:
<identifier>[<ri>][<ci>] ← <value>
Where ri stands for row index and ci stands for column index.
Python
2D array initialization
student_attendance = [[None] * 10] * 5
2D array assignment
student_attendance[8][3] = True
13
File Handling
File Opening
When opening a file, the mode of operation of the file should be stated as follows:
<operator> = open(<file identifier>, <file mode>)
The operator is used as a reference to the file opened. The file identifier will be the name of the file.
The following file modes are used:
r for data to be read from the file
w for data to be written to a file. In case the file does not exist, a new file will be created. Existing
data is overridden.
a for data to be appended to a file. Existing data will not be overridden
A file can only be opened in one mode at a time.
File Closing
When a file is no longer in use, it is closed as follows:
<operator>.close()
Python
Writing to a file
file_b.write("Jane Doe")
Closing file
file_a.close()
14
Procedures and Functions
Procedures and functions are always defined at the top of the program.
Procedures
Python does not support procedures.
Functions
In Python, functions can either output many values or only return a singular value.
Function without parameters are defined as follows:
def <identifier>():
<statements>
Function with parameters are defined as follows:
def <identifier>(<par n>):
<statements>
When used, par n is the identifier for the parameters of the function. These will be used as variables
in the statements of the function.
Function calls are not a complete program statement. Instead, functions should only be called as a
part of an expression.
Python
Defining function
def sum_square(num_1, num_2):
print(num_1 ** 2 + num_2 ** 2)
Using function
print("Sum of squares", sum_square(5, 10))
15