lOMoARcPSD|61770288
GCE Advanced Level ICT Python- English mediam
python and R (University of Colombo)
Scan to open on Studocu
Studocu is not sponsored or endorsed by any college or university
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
A/L ICT
2019 onwards
New syllabus
Advanced Level
Sri Lankan
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
1 | Page
Arithmetic Operators
+ :- Addition
- :- Subtraction
* :- Multiplication
/ :- Division
// :- Integer division
** :- Exponentiation
% :- Modulus
Example
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
2 | Page
Relational / Comparison operators
> :- Greater than
< :- Less than
> = :- Greater than or equal to
< = :- Less than or equal to
= = :- Equal
! = :- Not equal
Example
Example
Print (5 < = 5) True
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
3 | Page
Logical Operators
1) AND
2) OR
3) NOT
Example
Shift Operators
>> :- right-shift
<< :- left- shift
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
4 | Page
Example
5>>2 101|2 12 110
5<<2 101|2 101002 2010
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
5 | Page
Variables
In programming, variables are used to
store data.
Assignment operator
a=5 Value
Variable
Memory space is allocated for storing an
integer value.
The value” 5” is assigned to variable “a
“and stored in the memory location.
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
6 | Page
Example
Explanation
A memory location for variable” a” is
created.
The value 2 is assigned to variable
“a” and stored in memory.
A memory location for variable” b” is
created.
The value 3 is assigned to variable “b”
and stored in memory.
The values stored in variable “a” and
“b” are retrieved from memory and
added together and assigned to
variable “c” and stored in memory.
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
7 | Page
input ( ) function
input () function is used to enter user input
through keyboard.
It takes value as a string data type.
Example
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
8 | Page
Data type conversion
String Integer
String Float
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
9 | Page
Concatenation
Explanation
X=3
Y=2 String Numeric value
Print (“x+y=’, (x+y))
Concatenation operator
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
10 | P a g e
Conditional statements
if condition:
Statement 1
else:
Statement 2
Program checks the condition.
If the condition is true, then the statement 1 is
executed.
Otherwise statement 2 is executed.
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
11 | P a g e
Nested if
if
if
if
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
12 | P a g e
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
13 | P a g e
Electricity Meter Reading Question
50 units:- 300/=
51 – 100 units:- 5/= per each
>100 units :- 20/= per each
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
14 | P a g e
Operations precedence
print (5*3+2)
1
2
print(5*2-4/2+2*2)
1
4 5
3
2
print(7%2+5)
1
2
print(3**2+1)
1
2
print(3**2+4/2-1)
1
4
2
3
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
15 | P a g e
Bitwise AND, OR, XOR
Bitwise AND
& :- Ampersand
print (5 & 4) = 4 (output)
101 & 100 101
100
100 4
Bitwise OR
| :- Vertical bar
print(5|4) = 5 (output)
101 | 100 101
100
101 5
Bitwise XOR
^ :- Caret (Hat) 101
print (5 ^ 4) = 1 (output) 100
101 ^ 100 001 1
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
16 | P a g e
Combined Assignment Symbols
+=
-=
*=
/=
// =
** =
%=
Examples
a=5
a+=2 a= a+2
print(a)
Output: 7
a=8
a%=3 a= a % 3
print(a)
Output: 2
a=2
a**=3 a= a**3
print(a)
Output: 8
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
17 | P a g e
a=”python”
a+=”is not a snake”
print(a) Output: python is not a snake
Identifiers
Valid Invalid identifiers
identifiers
a ab No space
ab a-b
a_b a#b No special
_a a@b characters
a_ a$b
a2 2a-Does not start with digit
if
for
else No keywords
while
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
18 | P a g e
Key words OR Reserved Words
Keywords cannot be used as ordinary Identifiers.
and elif pass exec
del global yield in
from rr break ralse
not with except continue
while assert import finally
as else print is
try if class return
lambda for def
Integer
a=3
print (a)
Float
a=3.4
print (a)
Boolean ( capital must)
a=True a=False
print(a) print(a)
String
a=”python” a=’python’
print(a) OR print(a)
Output: python
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
19 | P a g e
Note
a=’nimal’s address’ ERROR
print(a)
a=”nimal’s address” Output: nimal’s address
print (a)
List
a= [3, 5, 6, 1, 9]
print(a)
Output: [3, 5, 6, 1, 9]
a= [3, 5, 6, False, ‘Python’]
print(a)
Output: [3, 5, 6, False, ‘Python’]
a=[2, 4, [1,5], 9]
print (a)
Output: [2, 4, [1, 5], 9]
a= [[[[[]]]]]
print (a)
Output: [[[[[]]]]]
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
20 | P a g e
a=[2,3]
b= [1, 8, 7]
print (a+b)
Output: [2, 3, 1, 8, 7]
a=[4,5,7,2,3,9]
print (a [3])
Output: 2
a= (4, 5, 7, 2, 3, 9)
print (a [-2])
Output: 3
a=[4,5,7,2,3,9]
print (a [1:5])
Output: [5, 7, 2, 3]
a=[4,5,7,2,3,9]
print (a [2 :])
Output: [7, 2, 3, 9]
a=(4,5,7,2,3,9)
print (a (:3))
Output: [4, 5, 7]
a= [4, 5, 7, 2, 3, 9]
print (a [:3])
Output: [4, 5, 7]
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
21 | P a g e
a=[4,5,7,2,3,9,1,8]
print (a [1::2])
Output: [5, 2, 9, 8]
a=[4,5,7,2,3,9,1,8]
print (a [::2])
Output: [4, 7, 3, 1]
a=[4,5,7,2,3,9,1,8]
print (a [1:6:2]) Output: [5, 2, 9]
a= [4,5,7,[2,3,9],1,8)
print (a [3][2])
Output: 9
a=[4,5,7]
a [1] = 6
print (a)
Output: [4, 6, 7]
Note
List
Array Tuple
Dictionary
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
22 | P a g e
Tuple
i. t=(2,3,5,4,1)
print (t [2])
Output: 5
ii. t=(2,3,5,4,1)
print (t [1:3])
Output: (3, 5)
iii. t1=(2,3)
t2=(1,4)
print(t1+t2)
Output: (2, 3, 1, 4)
iv. t=(2,3,4,’Python’,(),(1,2))
print(t)
Output: (2, 3, 4, Python, (), (1, 2))
Note
a= (3, 6, 4, 1) ERROR
a= [2] =7
print (a)
List - Mutable object
Tuple-Immutable Object
Dictionary - Mutable object
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
23 | P a g e
Dictionary
Dictionary must be in pair.
d={key: value}
d={4:‘Apple’,1:‘Orange’,3:‘Banana’}
Print (d)
Output: {1: ‘Orange’, 3: ‘Banana’, 4: ‘Apple’}
d={4:‘Apple’,1:‘Orange’,3:‘Banana’}
print(d [3])
Output: Banana
d={4:‘Apple’,1:‘Orange’,3:‘Banana’}
d [1] = ‘Strawberry’
print (d)
Output: {1:‘Strawberry’, 3:‘Banana’, 4:‘Apple’}
d = {‘A’:‘Apple’;‘b’:‘Ball’}
print (d [‘A’])
Output: - Apple
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
24 | P a g e
Computer Programming Languages
C
C++
Python
Java
C#
Pascal
FORTRAN
COBOL
Generations of computer programming Languages
1GL
2GL
3GL
4GL
1GL
Also called machine Language.
Programs are written in binary/machine code.(1,0)
Execution of programs is very fast.
No programs translation needed.
Difficult to write/test programs comparing with 3GL/4GL.
Tied up with the computer architecture.
Binary 10001101
10101110
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
25 | P a g e
2GL
Also called Assembly Language.
Programs are written in mnemonics instructions
Used to write programs for CPU.
Assembler is needed to execute programs
Difficult to write/test programs.
Tied up with the computer architecture.
Mapping between assemble instructions and machine
instructions is 1-1.
ADD 2, 3, R1
SUB 2, 4, R2
Note
Programmer 1GL/2GL computer
1GL and 2GL are collectively called ‘Low Level
programming Languages’.
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
26 | P a g e
3GL
Example: C, C++, Java, Pascal.
Programs are written using mathematical symbols and
natural Language words.
Execution of programs is slow.
Valid program translator needed to execute programs.
Easy to write/test programs in comparing with 1GL/2GL.
4GL
Example – SQL
Programs are written using natural Language word.
Execution of programs is very slow.
Valid program translator needed.
Easy to write/test programs comparing with 1GL/2GL
4GL is a Language and its environment that is used to
develop software rapidly.
Note
Programmer 3GL/4GL Computer
3GL and 4GL collectively called ‘High Level programming
Languages’.
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
27 | P a g e
Program Translators
for i in
range (1, 10) program 101011
print (i) translator 111000
Source code Machine code/ object code
A program translator is a computer program that performs
the translation of a program written in a given programming
language into machine understandable code/object code.
Three types of Translators
Compiler
Interpreter
Assembler
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
28 | P a g e
Compiler
Eg:-Java, c, c++
for i in
range (1, 10) compiler 101011
print (i) 111000
A compiler translates source code into object code.
First the entire program is converted into machine code at a
time.
Then, the compiled code can be executed on the computer.
Interpreter
Eg:- Python
for i in
range (1, 10) Interpreter 101011
print (i) 111000
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
29 | P a g e
An interpreter translates and executes
source code.
One instruction at a time, is converted into
machine code and executed by the
processor.
Assembler
A computer program which translates
assembly language mnemonics to an object
code/machine code format.
Assembler uses either compiler or
interpreter fashion.
Assembler is used in 2GL.
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
30 | P a g e
Looping structure
If a program or a part of program is repetively
executed it is called “loop”.
Eg: - while loop, for loop.
While loop
while (condition)
Statements
Program first check the condition.
If the condition is true, statements are
executed.
Otherwise it terminates the loop.
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
31 | P a g e
Odd number
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
32 | P a g e
Square number
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
33 | P a g e
Sum of the first integers
Note
If End = ‘ ‘ used and print on next
line(\n)
Example
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
34 | P a g e
Factorial program
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
35 | P a g e
For loop
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
36 | P a g e
Break / Continue
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
37 | P a g e
User defined function
def function name():
A function must be called otherwise, it will
not work.
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
38 | P a g e
Parameter passing
User defined function
No-return Return
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
39 | P a g e
No-return type
Return type
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
40 | P a g e
Nested loop
for…. while…
for…. while….
for….
while….
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
41 | P a g e
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
42 | P a g e
Multiplication table
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
43 | P a g e
All the multiplications of 1-12 will visible up to
16 in order.
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
44 | P a g e
Text file handling
.py
.txt
Write
R=Read mode
W=write mode
A=append mode
Open function
f=open(‘[Link]’ )
print (f)
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
45 | P a g e
open(): The built in functions takes the name of
the file as a parameter and returns a file object.
OR
f=open (‘[Link]’’r’)
print(f)
Redline()
f=open (‘[Link]’)
print ([Link] ())
readline (): reads characters from the file until it
get to newline and returns the results as string.
Read ()
f=open(‘[Link]’)
print ([Link] ())
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
46 | P a g e
Strip()
f=open (‘[Link]’)
line= [Link] ()
a =[Link] ()
print (a)
[Link]()
Strip returns a copy of the string in which all
characters have been stripped from the beginning
and the end of the string.
Write () function
f=open(‘[Link]’,’w’)
[Link](‘welcome’)
[Link] ( )
Replaces the text on [Link] by welcome
f=open (‘[Link]’,’w’)
[Link] (‘python\n’)
[Link] (‘programming’)
[Link] ()
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
47 | P a g e
Output
python
programming
f=open(‘[Link]’,’a’)
[Link] (‘welcome’)
[Link] ( )
Output
python
programming welcome
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
48 | P a g e
Example
f1=open (‘[Link]’,’r’)
f2=open (‘[Link]’,’w’)
for line in f1:
data= (line. strip ()).split (“, “)
total=float (data [1]) +float (data [2])
[Link] (‘%75-%4d\n %( data[a], total)’)
[Link] ()
[Link] ()
[Link]
Nimal, 30, 60
Saman, 80, 45
Upali, 100, 80
[Link]
Nimal-90
Saman -125
Upali-180
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
49 | P a g e
Example
f1=open (‘[Link]’,’r’)
f2=open (‘[Link]’,’w’)
line=[Link] ()
while(line):
data=line. strip ().split (“,”)
total=float (data [1]) +float (data [2])
[Link](‘{},{ },{ },{ },\n
format(data[0],data[1],data[2],total))
line=[Link] ()
[Link] ( )
[Link] ()
Output
{ },{ },{ },{ }
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
50 | P a g e
[Link]
Nimal, 30, 60
Saman, 80, 45
Upali, 100, 80
[Link]
Nimal, 30, 60, 90.0
Saman 80, 45,125.0
Upali 100, 80,180.0
Consider the following python program
data = [5, I, 23, 10,-3]
def fun (a):
i, c=1, a[0]
while i<len(a):
if (a[i]>c):
c=a(i)
i=i+1
return
print (fun (data))
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
51 | P a g e
Programming Errors / Bugs
Compile time / Syntax error
Run – time error
Logical / Semantic error
Compile time / Syntax error
A syntax error is an error in the syntax of a sequence of
characters.
Syntax errors are detected at compile time.
A program will not compile until all syntax errors are
corrected.
Shows error message.
Example
x=2
print(x)
x=2
print(x)
for i in range (1,10)
print (i)
Run –time error
A type of error that occurs during the execution of a
program is known as run time error.
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
52 | P a g e
Run time errors are usually more difficult to find and fix
than syntax errors.
Shows error message.
Example
x=0
y=0
print (x/y)
Logical / Semantic Error
Unexpected results occur.
These are valid code the compiler understands but they do
not have what programmer intended.
Does not show error message.
Escape Sequence / Characters
print (“Python\n Programming\n Language”)
In –New line
Output
Python
Programming
Language
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
53 | P a g e
It –tab space
print (“Python\t Programming\t language”)
Output
Python Programming Language
\’ - Single quote
print (Vimal\’s address)
Output
Vimal’s address
\” Double Quote
print (‘CPU \”Central Processing Unit\”)
Output
CPU “Central Processing Unit”
import math
print (sum ((3, 6, 6, 5, 7)))
Output
27
import math
print (abs (-56.0)) #absolute value (Comment)
Output
56.0
import math
print (max (5, 8, 3))
Output 8
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
54 | P a g e
import math
print (min (5, 8, 3))
Output
3
import math
print (math. Sin (45))
Output
.tan
.cos
import math
print ([Link] (6.567))
Output
6
import math
print ([Link] (2.567))
Output
3
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
55 | P a g e
import math
print ([Link] (2.567))
#Truncate (drop decimal digits)
Output
2
import math
print (round (2.567))
#rounding off
Output
3
import math
print (‘%2f’%2.567, ‘{0:.2f}’)
Format (2.567)
Output
2.57 2.57
import math
print (“%2f” %( 1/3))
Output
0.33
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
56 | P a g e
Example 01
x=2
y=5
for j in range (o, y, x):
print (j)
print (x+y)
print (“Done”)
Output
0
7
2
7
4
7
Done
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
57 | P a g e
Example 02
ans=0
for i in range (1, 11):
ans=ans+i*i
print (i, end=’ ‘)
print (ans)
Output
1 2 3 4 5 6 7 8 9 10
385
Example 03
a= [‘cat’,’window’,’moon’]
for x in a:
print (x,len(x))
Output
cat 3
window 6
moon 4
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
58 | P a g e
Example 04
a = [66.25, 333, 333, 1, 1234.5]
print ([Link] (333))
Output
2
Example 05
a= (5, 7, 8, 4)
[Link] ( )
print (a)
Output
[4, 8, 7, 5]
Set
Set is a collection which is unordered and unindexed.
No duplicate members.
s={4,6,5,3,1}
Print (s)
Output
{1, 3, 4, 5, 6}
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
59 | P a g e
s={}
print(s)
Output
{}
s={3,5,9,1,4} ERROR
print(s [1])
s1={3,5}
s2= {1, 6, 5}
print (S1= =S2)
Output
False
s1={3,5}
s2= {3, 5}
print (S1= =S2)
Output
True
s1={3,5}
s2= {5, 3}
print(S1==S2)
Output
True
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])
lOMoARcPSD|61770288
60 | P a g e
Note
List: - List is a collection which is ordered and changeable.
Allow duplicate members (Mutable)
Tuple: - Tuple is a collection which is ordered and
unchangeable. Allow duplicate members. (Immutable)
Dictionary: - Dictionary is a collection which is unordered,
changeable and indexed. No duplicate members. (Mutable)
Downloaded by Kenneth Magnus (kennethmagnus29@[Link])