0% found this document useful (0 votes)
4 views

Chapter13programming&Data Representation

Uploaded by

frankyuang666
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter13programming&Data Representation

Uploaded by

frankyuang666
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

CAMBRIDGE INTERNATIONAL AS LEVEL

COMPUTER SCIENCE
9608
For examination in May 2019
CAMBRIDGE INTERNATIONAL AS & A2 LEVEL
COMPUTER SCIENCE 9608
CAMBRIDGE INTERNATIONAL AS & A2 LEVEL
COMPUTER SCIENCE 9608
CAMBRIDGE INTERNATIONAL AS & A2 LEVEL
COMPUTER SCIENCE 9608

?
The maximum number of marks is 75 for each paper.
CIC
Computer
Science 9608

Flair

• a way of doing things that is interesting and


shows imagination
CIC
Computer
Science 9608

Flair

Discipline

• the ability to control your own behaviour, so that


you do what you are expected to do
CIC
Computer
Science 9608

Flair

Discipline

Academic Rigour

• great care and thoroughness in


making sure that something is correct
WEEKLY SCHEDULE
Sep 25 26 27 28 29 30 Oct 1 2 3 4 5 6 7
Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun
Course Chapter 13 Sport

Task12.01
Paper ESQ P172-175 Exam-style Question P211
P158

Python Python Crash Course 3-11 Sport

Chapter 13 | Programming & FLAIR


DISCIPLINE
Data Representation ACADEMIC RIGOUR
PAPER TASK TODAY
1. Term Explanation ;
2. TASK13.01 P185
TASK13.02 P187
TASK13.03 P189

Chapter 13 | Programming & FLAIR


DISCIPLINE
Data Representation ACADEMIC RIGOUR
13.01 Programming languages

Python

VB.NET

Pascal

Chapter 13 | Programming & FLAIR


DISCIPLINE
Data Representation ACADEMIC RIGOUR
Conceived by Guido van Rossum in 1989
Python
Version 2.0 released in 2000

Python 3.0 in 2008

July 21, 2018 …


|import this
import this|
a multi-paradigm programming language

It fully supports:

 Objective-oriented programming

 Structured programming

Using extensions, Python also supports:

 Logical programming, e.g. Prolog (Chapter 29)

 and so on …
Key

characteristics:
Page 177
Key

characteristics:
1. Every statement must be on a separate line.

Page 177
Key

characteristics:
1. Every statement must be on a separate line.

2. Indentation is significant: 'off-side rule’.

Page 177
Key

characteristics:
1. Every statement must be on a separate line.

2. Indentation is significant: 'off-side rule’.

3. Keywords are written in lower case.

Page 177
Key

characteristics:
1. Every statement must be on a separate line.

2. Indentation is significant: 'off-side rule’.

3. Keywords are written in lower case.

4. Python is case sensitive.

Page 177
Key

characteristics:
1. Every statement must be on a separate line.

2. Indentation is significant: 'off-side rule’.

3. Keywords are written in lower case.

4. Python is case sensitive.

5. Everything in Python is an object (Chapter

27).

Page 177
封装性 | Encapsulation
继承性 | Inheritance
多态性 | Polymorphism
Key

characteristics:
1. Every statement must be on a separate line.

2. Indentation is significant: 'off-side rule'.

3. Keywords are written in lower case.

4. Case sensitive

5. Everything in Python is an object (Chapter

27).

6. Code makes extensive use of a concept [0] [1] [2] [3] [4] [5] [6]

called 'slicing' (Section 13.08, Page 203). A B C D E F G


[-7] [-6] [-5] [-4] [-3] [-2] [-1]
Page 177
# Original code
a = 1
b = 2
c = a + b

# C Addition
Assign 1 to a
Assign 2 to b
Key call binary_add(a, b)
Assign the result to c
For Python, here the interpreter knows only that 1 and 2 are objects, but not what type
of object they are. So the The interpreter must inspect PyObject_HEAD for each
characteristics: variable to find the type information, and then call the appropriate summation routine
for the two types. Finally it must create and initialize a new Python object to hold the
1. Every statement must be on a separate line. return value. The sequence of events looks roughly like this:

# Python Addition
2. Indentation is significant: 'off-side rule'.
# Assign 1 to a
1.1 Set a->PyObject_HEAD->typecode to integer
3. Keywords are written in lower case.
1.2 Set a->val = 1

4. Case sensitive # Assign 2 to b


2.2 Set b->PyObject_HEAD->typecode to integer
2.2 Set b->val = 2
5. Everything in Python is an object (Chapter
# Call binary_add(a, b)
27). 3.1 find typecode in a->PyObject_HEAD
3.2 a is an integer; value is a->val
3.3 find typecode in b->PyObject_HEAD
6. Code makes extensive use of a concept 3.4 b is an integer; value is b->val
3.5 call binary_add(a->val, b->val)
called 'slicing' (Section 13.08). 3.6 result of this is result, and is an integer.

# Create a Python object c


7. Programs are interpreted (Chapter 7). 4.1 set c->PyObject_HEAD->typecode to integer
Declaration
Pseudocode Flowchart Python
of variables

Syntax DECLARE <identifier> : <dataType> No variable declarations


Definition

A whole number

DECLARE Number1 : INTEGER # Number1 of type Integer


Code DECLARE YourName : STRING # YourName of type String
DECLARE N1, N2, N3 : INTEGER # N1, N2, N3 of type integer
Example DECLARE Name1, Name2 : STRING # Name1, Name2 of type string

A sequence of alphanumeric characters

13.02 Programming basics


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Declaration and
assignment of Pseudocode Flowchart Python
constants

Syntax CONSTANT <identifier> = <value> <identifier> = <value>


Definition

Code CONSTANT Pi = 3.14 PI = 3.14


Example

13.02 Programming basics


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Assignment
Pseudocode Flowchart Python
of variables

Syntax <identifier> ← <expression> <identifier> = <expression>


Definition

Code A ← 34 A = 34
Example B ← B + 1 B += 1
An operator claims: B = B + 1

13.02 Programming basics


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Example 1. Swap numbers.
a) What is the result of the pseudocode hereunder?
a ← 9
b ← 3
a ← a + b
b ← a – b
a ← a – b
OUTPUT "a=", a, ", b=", b
b) Test your answer in Python IDLE.

13.02 Programming basics


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Arithmetic operators Pseudocode Python
parentheses () ()
exponentiation ^ **
multiplication * *
division / /
integer division DIV //
modulus MOD %
addition + +
subtraction - -

Rules of precedence: define the order of the calculations to be performed

13.02 Programming basics


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Question 13.01 (Page 183)
a) Evaluate each of the following expressions:
4*3-3^2
(4*3-3)^2
4*(3-3)^2
4*(3-3^2)
b) Test your answer in Python IDLE.

13.02 Programming basics


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Output to
Pseudocode Flowchart Python
the screen

Syntax OUTPUT <string> print(<printlist>)


OUTPUT <identifier(s)> print(<printlist>, end = '')
Definition
Avoid moving to the next line after
the output

OUTPUT "Hello ", YourName, ". print("Hello ", YourName,


Code Your number is ", Number1 // ". Your number is ", Number1)
newline
Example OUTPUT "Hello "// no newline print("Hello ", end = '')

13.02 Programming basics


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Example 2. Problem solving and placeholder.
a) Follow the coursebook, please code in Python IDLE:
print ("Hello ",YourName,". Your number is ",Number1)

b) Does it perform like an normal printer? How to improve?


The variables to be printed are represented by sequential numbers in {} (or %<datatype>) in the
message string and the variables are listed in the correct order after the string, separated by commas.

c) Try placeholder method for output:


YourName = "John"
Number1 = 9
print ("Hello {0}. Your number is {1}.".format(YourName, Number1))
print ("Hello %s. Your number is %d." % (YourName, Number1))

13.02 Programming basics


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Input from
Pseudocode Flowchart Python
the user

All input is taken to be a string

Syntax INPUT string <identifier>


<identifier> =
Definition input(string)

Code INPUT "Prompt: " A A = input("Prompt: ")


Example

13.02 Programming basics


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Comments Pseudocode Flowchart Python

# comment
Syntax // comment '''multi-line
Definition comment'''

# this is a comment
Code // this is a comment '''this is a multi-line
Example comment'''

13.02 Programming basics


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Description of data Pseudocode Python

Logical values: True (1) / False (0) BOOLEAN bool

Whole signed numbers INTEGER int

Signed numbers with a decimal point REAL float

Monetary value CURRENCY Not available

Not available as a built-in data


Date value DATE
type

Not available
A single alphanumeric character CHAR
Represented as a string of length 1

str (stored as ASCI I but Unicode


STRING Use single (') or double (") strings are also available). Use
A sequence of alphanumeric characters
quotation marks to delimit a single ('), double (") or triple ('"
(a string) string. or """) quotation marks to delimit a
string.

13.03 Data types


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Operation Pseudocode Python
equal = =

not equal <> <>

greater than > >

less than < <

greater than or equal to >= >=

less than or equal to <= <=

AND (logical conjunction) AND and

OR (logical inclusion) OR or

NOT (logical negation) NOT not

13.04 Boolean expressions


FLAIR
DISCIPLINE
ACADEMIC RIGOUR
IF…THEN Pseudocode Flowchart Python

The THEN keywords is replaced


IF <Boolean expression> by a colon (:)
Syntax THEN if <Boolean expression>:
Definition ENDIF
<statement(s)> <statement(s)>
Indentation is used to show which statements
form part of the conditional statement.

IF x < 0
Code THEN if x < 0:
Example ENDIF
OUTPUT "Negative" print ("Negative")

13.05 Selection
FLAIR
DISCIPLINE
ACADEMIC RIGOUR
IF…
THEN… Pseudocode Flowchart Python
ELSE
The else keyword must line up
with the corresponding if keyword

IF <Boolean expression>
THEN if <Boolean expression>:
Syntax <statement(s)> <statement(s)>
Definition ELSE
<statement(s)>
else:
<statement(s)>
Indentation is used to show which statements
ENDIF form part of the conditional statement

IF x < 0
THEN if x < 0:
Code OUTPUT "Negative" print ("Negative")
Example ELSE
OUTPUT "Positive"
else:
print ("Positive")
ENDIF

13.05 Selection
FLAIR
DISCIPLINE
ACADEMIC RIGOUR
Nested IF Pseudocode Flowchart Python
elif (an abbreviation of else if)
IF <Boolean expression> must line up with the corresponding if
THEN
<statement(s)> if <Boolean expression>:
ELSE <statement(s)>
Syntax IF <Boolean expression>
THEN
elif <Boolean expression>:
Definition <statement(s)> <statement(s)>
else:
ELSE
<statement(s)> <statement(s)>
ENDIF
There can be as many elif
ENDIF
parts to this construct as required

IF x < 0
THEN
OUTPUT "Negative" if x < 0:
ELSE print ("Negative")
Code IF x = 0
THEN
elif x = 0:
Example ELSE
OUTPUT "Zero" print ("Zero")
else:
OUTPUT "Positive" print ("Positive")
ENDIF
ENDIF

13.05 Selection
FLAIR
DISCIPLINE
ACADEMIC RIGOUR

You might also like