Python 2
Python 2
GCE (A/L) ICT Training for Teachers (Last Updated on 21st March 2011)
Outcomes
Introduction to Python
Data Types and Calculations
Selection
Repetition
String Handling
Functions
Lists
File Handling
Classes
Programming Languages
Programming Languages
Assembly
Source
Code
C8
B7
23A7
D7
AA
89
DD
89D23
A9
F4
A9
...
Machine Code
What is a Compiler?
Python
Python Language
Python Keywords
History
Operator Precedence
Operator
Description
Associatively
* / %
Multiplication/division/modulus
left-to-right
+ -
Addition/subtraction
left-to-right
Arithmetic Operators
(Python 3 only)
MCQ Question 41
MCQ Question 41
Numerical Calculations
10+20/2
(10+20)/2
Calculations
inches.py
Calculations
celcius.py
Selection
if (cond) then
Statement A
else
Statement B
endif
Selection
if (cond) then
Statement A
else
Statement B
endif
if cond:
Statement A
else:
Statement B
if statement
if statement
Nested if statement
if (test1) then
block1
else if (test2) then
block2
else
elseBlock
endif
endif
Nested if statement
if (test1) then
block1
else if (test2) then
block2
else
elseBlock
endif
endif
if test1:
block1
elif test2:
block2
else:
elseBlock
Nested If Statement
Grades
Nested If Statement
MCQ Question 42
MCQ Question 42
If 10<4 or 10!=4
false or true
true
j = j-y
j = 10-4
j=6
GCE (A/L) ICT Training for Teachers
Repetition
do while (cond)
execute Statement
end while
Repetition
do while (cond)
execute Statement
end while
while cond:
execute Statement
while
while1.py
while
while2.py
while
for r in range(1,10,2):
print(r)
for r in country:
print(r)
S
R
I
L
A
N
K
A
MCQ Question 43
MCQ Question 43
MCQ Question 43
MCQ Question 43
MCQ Question 44
MCQ Question 45
String Manipulation
List Manipulation
List Manipulation
len(mylist) #4
mylist.append(89) # adds 89
# [10, Ajith, 67.3, 78.2, 89]
mylist.sort() # sorts list
mylist.reverse() # reverses list
Question II 3
AQ3c.py
p
y
t
h
o
n
Question II 3
pythonprogramming
(one letter per line)
GCE (A/L) ICT Training for Teachers
AQ3d.py
Part B Question 5
1 Cat 3
2 Dog 3
3 Rat 3
BQ5b.py
Functions
Functions
Example
import pythonprg
pythonprg.func1(3)
Setting PYTHONPATH
StartCMD
SET PYTHONPATH=
c:\python27;c:\python27\programs
Importing modules from IDLE
import sys
sys.path.append(c:\\python27\\programs)
Part B Question 5
BQ5b2.py
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
5 x 11 = 55
Question II 3
Python Code
x=1
total = 0
while x <= 10:
marks = int(input("Enter marks : "))
total = total + marks
x=x+1
avg = total/(x-1)
print "total ",total
print "avg ", avg
if avg>50:
print "Good"
else:
print "Bad"
File Handling
Part B Question 5
BQ5c.py
A person working
In a company
Person
EmpNo
Name
Address
BasicSal
OtHrs
OtRate
CalcOtAmt
CalcNetSal
Properties
Methods
Blue Print
House2
House3
Classes
# Shape.py
class Rectangle:
def __init__(self,x,y):
def display(self):
import Shape
rec1 = Shape.Rectangle(10,20)
rec1.display()
GCE (A/L) ICT Training for Teachers
Classes
# Shape.py
class Rectangle:
def __init__(self,x,y):
def display(self):
Circle
MCQ 50
MCQ 50
MCQ 49
Exercises
Exercise