Sunteți pe pagina 1din 83

Mr Nuwan Kodagoda

Head/Department of Information Technology


Sri Lanka Institute of Information Technology

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

GCE (A/L) ICT Training for Teachers

Programming Languages

Programming Languages

There are hundreds of programming


languages.
Very broadly these languages are
categorized as
o
o

Low Level Languages


High Level Languages

Low Level Languages

More closer to the Architecture of the


computer.
Very difficult for us humans to use.
Machine Code
Memory
Addresses

Assembly

High Level Languages

High Level Languages are closer to


human languages.
Dim radius as integer
Dim area as integer
radius = txtRadius.text
area = 22/7*radius*radius
msgbox area

How a Computer Program works

The Program you write is executed by


the CPU.
Since the CPU can only understand
machine code your program needs to
be converted into machine code.

How a Computer Program works

Dim radius as integer


Dim area as integer
radius = txtRadius.text
area = 22/7*radius*radius
msgbox area

Source
Code

C8
B7
23A7
D7
AA
89
DD
89D23
A9
F4
A9
...

Machine Code

How a Computer Program works

The software that does the conversion


to machine code comes in two flavours.
Compilers
Interpreters

What is a Compiler?

A compiler converts your entire


program to machine code at one go.
After compilation you have an
executable file.
e.g. an EXE, COM file for PCs

How an Interpreter Works

An Interpreter converts your program to


machine code one instruction at a time.

Python

Python is an easy to learn, powerful


programming language.
It has efficient high-level data structures
and a simple but effective approach to
object-oriented programming.
Python is an ideal language for
scripting and rapid application
development in many areas on most
platforms.
GCE (A/L) ICT Training for Teachers

Python Language

No variable, data type declaration


necessary (Dynamically Typed)
Case Sensitive variables
Indentation of 4 characters used for
block structures (e.g. if, while,
functions)
Program extension is .py
Runs on multiple platforms
Open Source
GCE (A/L) ICT Training for Teachers

Python Keywords

Like most modern languages Python


has only a few keywords.

GCE (A/L) ICT Training for Teachers

History

Developed in the early


1990s by Guido van
Rossum.
The name Python was
based a popular TV show
called the Monty Python.

GCE (A/L) ICT Training for Teachers

Which version of Python should I use?

There are two versions of Python


available
Python 2 the latest is 2.71
Python 3 the latest is 3.2
For the ICT exam use Python 2
Some differences e.g. in Python 3 the
print is a function so print 10, 20 has to
be written as print(10,20)

GCE (A/L) ICT Training for Teachers

Downloading Python and Installing

GCE (A/L) ICT Training for Teachers

Operator Precedence

Operator

Description

Associatively

* / %

Multiplication/division/modulus

left-to-right

+ -

Addition/subtraction

left-to-right

GCE (A/L) ICT Training for Teachers

Arithmetic Operators

(Python 3 only)

GCE (A/L) ICT Training for Teachers

MCQ Question 41

r=11; y=2.5; c=4 have been assigned to


three variables
Find the value of r%3*c+10/y

GCE (A/L) ICT Training for Teachers

MCQ Question 41

r=11; y=2.5; c=4 have been assigned to


three variables
Find the value of r%3*c+10/y
11%3*4+10/2.5
2*4+10/2.5
8+10/2.5
8+4.0
12.0

GCE (A/L) ICT Training for Teachers

Numerical Calculations

10+20/2
(10+20)/2

GCE (A/L) ICT Training for Teachers

Printing values in Python

print 10, Nadun, 1990


In Python 3 print is a function
print(10, Nadun, 1990)

GCE (A/L) ICT Training for Teachers

Input Data in Python 2

no1 = int(input(Enter an int Number : ))


no2 = float(input(Enter a float Number : ))
str = raw_input(Enter a string : )

GCE (A/L) ICT Training for Teachers

Calculations

Write a python program to input a


length in mm and convert it to inches.
25.4 mm = 1 inch

inches.py

GCE (A/L) ICT Training for Teachers

Calculations

Write a python program to input a


temperature in Celcius and convert it to
Fahrenheit.
fahrenheit = 9/5* celcius +32

celcius.py

GCE (A/L) ICT Training for Teachers

Selection

if (cond) then
Statement A

else
Statement B

endif

GCE (A/L) ICT Training for Teachers

Selection

if (cond) then
Statement A

else
Statement B

endif

if cond:
Statement A

else:
Statement B

GCE (A/L) ICT Training for Teachers

if statement

Write a program to input a mark and


display Pass if it is greater than or
equal to 50. Otherwise display Fail.

GCE (A/L) ICT Training for Teachers

if statement

Write a program to input a number.


Display if it is an even or an odd
number.
oddeven.py

GCE (A/L) ICT Training for Teachers

Nested if statement

if (test1) then
block1
else if (test2) then
block2
else
elseBlock
endif
endif

GCE (A/L) ICT Training for Teachers

Nested if statement

if (test1) then
block1
else if (test2) then
block2
else
elseBlock
endif
endif

if test1:
block1
elif test2:
block2
else:
elseBlock

GCE (A/L) ICT Training for Teachers

Nested If Statement

GCE (A/L) ICT Training for Teachers

Grades

Write a program to input the marks of a


student and calculate the grades.
> 75 A
65 to 75 B
55 to 64 C
45 to 54 S
< 45 - W

GCE (A/L) ICT Training for Teachers

Nested If Statement

Write a program to input an amount and


to calculate the discount as follows.
Discounts
>1000 - discount 10%
500-1000 - discount 5%
< 500 - discount 2%

GCE (A/L) ICT Training for Teachers

MCQ Question 42

What would be the output of this


program.

GCE (A/L) ICT Training for Teachers

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

GCE (A/L) ICT Training for Teachers

Repetition

do while (cond)
execute Statement

end while
while cond:
execute Statement

GCE (A/L) ICT Training for Teachers

while

Write a program to Print numbers 1 to 9

while1.py

GCE (A/L) ICT Training for Teachers

while

Write a program to Print numbers


1,3,5,7,9

while2.py

GCE (A/L) ICT Training for Teachers

while

Write a program to print the tables of 7.

GCE (A/L) ICT Training for Teachers

Using For loop in Python

Range command generates a list.


A list is similar to an array, but it can
store data of different types. It also
could contain another list.
range(1,10) produces the list
[1,2,3,4,5,6,7,8,9]
for r in range(1,10):
print(r)
GCE (A/L) ICT Training for Teachers

Using For loop in Python

range(1,10,2) produces the list


[1,3,5,7,9]

for r in range(1,10,2):
print(r)

GCE (A/L) ICT Training for Teachers

Using For loop in Python

Any list can be printed in the same way


Mylist = [Java,C#, VB6,
VB.NET,Python]
for r in Mylist:
print(r)
Java
C#
VB6
VB.NET
Python

GCE (A/L) ICT Training for Teachers

Using For loop in Python

This works the same for a string


country = SRI LANKA

for r in country:
print(r)

GCE (A/L) ICT Training for Teachers

S
R
I
L
A
N
K
A

MCQ Question 43

What would be the


output ?

GCE (A/L) ICT Training for Teachers

MCQ Question 43

What would be the


output ?
r= 5
s=3
r = r+s
r = 5+3 = 8
s = r+3=8+3
print 11
r<25 = 11<25 false
GCE (A/L) ICT Training for Teachers

MCQ Question 43

What would be the


output ?
r=8
s=11
r = r+s
r = 8+11 = 19
s = r+3=19+3=22
print 22
r<25 = 22<25 false
GCE (A/L) ICT Training for Teachers

MCQ Question 43

What would be the


output ?
r=19
s=22
r = r+s
r = 19+22 = 41
s = r+3=41+3=44
print 44
r<25 = 44<25 false
GCE (A/L) ICT Training for Teachers

MCQ Question 44

Select the correct pseudo


code for the given flow
chart

GCE (A/L) ICT Training for Teachers

MCQ Question 45

Which of the above


statements are correct

GCE (A/L) ICT Training for Teachers

String Manipulation

mystr = Sri Lanka


len(mystr)
mystr.upper() # upper case
mystr.upper() # lower case
mystr[0] # S
mystr[-1] # a

GCE (A/L) ICT Training for Teachers

List Manipulation

mylist = [10, Ajith, 67.3, 78.2]


mylist[0] # 10
mylist[3] # 78.2
mylist[-1] # 78.2
mylist[-2] # 67.3
sublist = mylist[1:3] # [Ajith, 67.3]
sublist = mylist[1:] # [Ajith, 67.3, 78.2]

GCE (A/L) ICT Training for Teachers

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

GCE (A/L) ICT Training for Teachers

Break and Continue

A break statement inside a block will


cause it to exit the loop
A continue statement inside a block will
skip the remaining instructions and go
to the beginning of the loop.

GCE (A/L) ICT Training for Teachers

Question II 3

AQ3c.py

GCE (A/L) ICT Training for Teachers

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

GCE (A/L) ICT Training for Teachers

Functions

A function that returns a value or does a


particular task
def cube(x):
return x*x*x
cube(10)

GCE (A/L) ICT Training for Teachers

Functions

Write a function that converts mm to


inches

GCE (A/L) ICT Training for Teachers

Example

Write a python program that returns the


maximum of two numbers
max(10,30) # should return 30

GCE (A/L) ICT Training for Teachers

Importing modules and functions

import pythonprg
pythonprg.func1(3)

from pythonprg import func1,


func2
func1(3)

Assume there is a program called


pythonprg.py which has two functions
named func1() and func2()
To use these functions from another
python program we need to do the
following.
GCE (A/L) ICT Training for Teachers

Setting PYTHONPATH

StartCMD
SET PYTHONPATH=
c:\python27;c:\python27\programs
Importing modules from IDLE
import sys
sys.path.append(c:\\python27\\programs)

GCE (A/L) ICT Training for Teachers

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

GCE (A/L) ICT Training for Teachers

Question II 3

GCE (A/L) ICT Training for Teachers

Pseudo Code for above flow chart


# Pseudo code
#x=1
# total = 0
# do while (x <= 10)
# input marks
# x=x+1
# total = total + marks
# end while
# avg = total/(x-1)
# if (avg>50) then
# display "Good"
# else
# display "Bad"
# endif

GCE (A/L) ICT Training for Teachers

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"

GCE (A/L) ICT Training for Teachers

File Handling

Python allows you to easily read and


write data files

GCE (A/L) ICT Training for Teachers

Write data to a file

Write a sample program to store data to


a data file where commas separate
values.
datafile = open ("marks.dat","w")
datafile.write(<str>)

GCE (A/L) ICT Training for Teachers

Read data from a file

Write a sample program to read data


from a data file where commas
separate values.
datafile = open ("marks.dat","r")
record = datafile.readline()
# split the data into a list for easier
manipulation. The comma is the seperator
mylist = record.strip(\n).split(,)

GCE (A/L) ICT Training for Teachers

Part B Question 5

BQ5c.py

GCE (A/L) ICT Training for Teachers

Object Oriented Program

Python Supports OO just like any other


modern programming language
Python code as usual is minimilistic
Both methods and attributes can be
accessed from outside.

GCE (A/L) ICT Training for Teachers

Classes and Objects

A person working
In a company

Person
EmpNo
Name
Address
BasicSal
OtHrs
OtRate
CalcOtAmt
CalcNetSal

Properties

Methods

Classes and Objects

You describe the


objects details in a
Class. A class is a blue
print of an object.
House1

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):

from Shape import Rectangle


rec1 = Rectangle(10,20)
rec1.display()
GCE (A/L) ICT Training for Teachers

Circle

Write a class to represent a circle


Calculate the area pi*r^2
Calculate the perimeter 2*pi*r

GCE (A/L) ICT Training for Teachers

MCQ 50

GCE (A/L) ICT Training for Teachers

MCQ 50

GCE (A/L) ICT Training for Teachers

MCQ 49

GCE (A/L) ICT Training for Teachers

Exercises

Write a program to input a series of


numbers terminated by -999. Calculate
and print the sum of the numbers
entered.

GCE (A/L) ICT Training for Teachers

Exercise

Write a program to input the sum of 10


numbers.
Modify the above program using a
break statement so that when the
number -999 is entered it will stop the
entering of marks
Modify the above program to ignore
negative numbers (except for -999).
Use the continue statement
GCE (A/L) ICT Training for Teachers

S-ar putea să vă placă și