L1 Part 3
L1 Part 3
1
Course outline
• Part 1 Introduction to Computing and Programming (first 2 weeks):
o Problem solving: Problem statement, algorithm design, programming, testing,
debugging
o Scalar data types: integers, floating point, Boolean, others (letters, colours)
o Arithmetic, relational, and logical operators, and expressions
o Data representation of integers, floating point, Boolean
o Composite data structures: string, tuple, list, dictionary, array
o Sample operations on string, tuple, list, dictionary, array
o Algorithms (written in pseudo code) vs. programs
o Variables and constants (literals): association of names with data objects
o A language to write pseudo code
o Programming languages: compiled vs. interpreted programming languages
o Python as a programming language
o Computer organization: processor, volatile and non-volatile memory, I/O
2
Course outline (may change a bit)
• Part 2 Algorithm design and Programming in Python (balance 11 weeks):
– Arithmetic/Logical/Boolean expressions and their evaluations in Python
– Input/output statements (pseudo code, and in Python)
– Assignment statement (pseudo code, and in Python)
– Conditional statements, with sample applications
– Iterative statements, with sample applications
– Function sub-programs, arguments and scope of variables
– Recursion
– Modules
– Specific data structures in Python (string, tuple, list, dictionary, array), with
sample applications
– Searching and sorting through arrays or lists
– Handling exceptions
– Classes, and object-oriented programming
– (Time permitting) numerical methods: Newton Raphson, integration,
vectors/matrices operations, continuous-time and discrete-event simulation
3
Variables and constants or literals
• Literal, or constant:
– Value of literals does not change (viz. cannot be changed)
– But one may read (or use/output/print) as many times
– Kinds of literals : integer, floating point, etc.
– Integers (or int in Python): -56
– Floating point numbers (or float in Python): -4.5
– Boolean (also in Python): ‘True’
– Strings (string in Python): ‘Hello World’
– Etc.
4
Variables and constants/literals
5
Variables and constants/literals
• Variables are a way to bind names with objects, or (re)assign value to names
• Bound values may be changed
Two different views of reassigning value
Output:
income 50000
updated income 51000
6
Variables and constants/literals
• How are literals and variables stored in Python, and consequence of binding variables to values:
message = ‘Hello, world’
gamma = 1
…
gamma = 2
int(‘10’)
7
Algorithms vs. programs
• Algorithms (written in pseudo code) vs. programs:
o Algorithm = a first step towards solving a problem
▪ Understand the logic or the method behind the solution
o Written in a somewhat generic ‘pseudo code’, with very few constructs
▪ Not bogged down by details concerning syntax of a programming language
o Algorithm uses features available in almost all languages
o May be analyzed for efficiency and for correctness (but not tested),
o Algorithm later re-written as a program in a programming language
o Program in a programming language use special features that may not available in
other languages
o Programs may be tested, debugged and documented
o One cam document algorithms as well
o Approach taken in this course:
▪ Write an algorithm to solve the problem
▪ Analyze it for correctness & efficiency
▪ Then re-write it in chosen programming language (e.g. Python), taking
advantage of special features that ease the programming task
8
A language to write pseudo code
• Example algorithm to determine minT(T1, T2, T3)
9
Assignment statements
• Assignment statements
Example of assignment statements
vol = height * length * width
tax = 30000 + 0.30*(INCOME-300000)
More generally:
v = expr
where v is variable, and
expr is arithmetic, relational or logical expression for example:
30000 + 0.30*(INCOME-300000)
INC > 200000
(marks > 49.99) and (marks < 60)
Note: expr is first evaluated and then resulting value assigned or bound to v
10
Role of ‘;’
• Role of ‘;’ is to separate two statements
For example: Python uses different
input(T1, T2, T3); mechanisms to separate
minT = T1; two statements, e.g.
if (T2 < minT) then minT = T2;
Write statements is on
if (T3 < minT) then minT = T3;
output(minT) separate lines
• Or, equivalently
input(T1, T2, T3); minT = T1;
if (T2 < minT) then minT = T2;
if (T3 < minT) then minT = T3;
output(minT)
• You may even skip the ‘;’ altogether, PROVIDED it is obvious that the two statements are distinct
E.g.
input(T1, T2, T3); minT = T1
if (T2 < minT) then minT = T2
if (T3 < minT) then minT = T3
output(minT)
11
Conditional statements
• Conditional statements
Examples:
if (T2 < T) then T = T2
if (INC > 100000) and (INC < 200001) then tax = 0.10 * (INC-100000)
More generally:
if C1 then S1
where condition C1 is Boolean valued logical expression. For example:
INC > 100000) and (INC < 200001)
(T2 < T)
12
Conditional statements
• Two variations of conditional statements:
if C1 then S1
and
if C1 then S1 else S2
where C1 is Boolean valued logical expressions, and S1, S2 are two statements
• Examples:
if (marks < 30) then grade4credit =‘F’
if (marks > 39.99) then grade4audit = ‘Pass’ else grade4audit = ‘Fail’
13
Conditional statements
• How about something like this?
if C1 then if C2 then S1 else S2
where C1, C2 are Boolean valued logical expressions, and S1, S2 are two statements
• Examples:
if (forAudit=True) then if marks > 39.9 then GR =‘AP’ else GR = ‘AF’
14
Conditional statements
• Consider what if S2 itself is a conditional statement
if C1 then if C2 then S1 else S2
where C1, C2 are Boolean valued logical expressions, and S1, S2 are statements
• Examples:
if (forAudit=True) then if marks > 39.9 then GR =‘AP’ else GR = ‘AF’
15
Conditional statements
• Compounding of multiple statements:
Why do we need this?
Multiple statements can be “bracketed together” to form ONE statement, or
A null statement may be bracketed together, or
Simply force an interpretation Python uses different
mechanisms to indicate
For example: that a sequence of
if x <= 1000 then [n = n + 1; x = 2 * x] statements is one
compound statement
is equivalent to
if x <= 1000 then [n = n + 1; x = 2 * x] else [ ]
16
Conditional statements
• The intended interpretation of:
if C1 then if C2 then S1 else S2
can be forced as follows:
if C1 then [if C2 then S1 else S2]
Or
if C1 then [if C2 then S1] else S2
Y < 0 Y >= 0
X < 0 yes no
X >= 0 - -
or
if x < 0 then [if y < 0 then output(“yes”)] else output(“no”)
Y < 0 Y >= 0
X < 0 yes -
X >= 0 no no
17
Conditional statements
• Example use of nested if-then-else statement:
INCOME Tax
Between 200000 & 300000 10000 plus 20% of INC excess of 200000
18
Conditional statements
• Example use of nested if-then-else statement:
INC Tax
Between 200000 & 300000 10000 plus 20% of INC excess of 200000
input(INC);
if INC > 300000
then Tax = 30000 + 0.30*(INC-300000)
else if INC > 200000 and INC ≤ 300000
then Tax = 10000 + 0.20*(INC-200000)
else if INC > 100000 and INC ≤ 200000
then Tax = 0000 + 0.10*(INC-100000)
else if INC ≤ 100000 then Tax = 0
19
Conditional statements
• Example use of nested if-then-else statement:
input(INC);
case [
INC > 300000: Tax = 30000 + 0.30*(INC-300000)
INC > 200000: Tax = 10000 + 0.20*(INC-200000)
INC > 100000: Tax = 0000 + 0.10*(INC-100000)
True: Tax = 0
]
20
Conditional statements
• Case statement in lieu of nested if-then-else statements
if C1 then S1 else [if C2 then S2 else [if C3 then S3 else [if C4
then S4 else [ ] ]]]
case [
<C1>: S1;
<C2>: S2;
<C3>: S3;
<C4>: S4
]
case [
INC > 300000: Tax = 30000 + 0.30*(INC-300000);
INC > 200000: Tax = 10000 + 0.20*(INC-200000);
INC > 100000: Tax = 0000 + 0.10*(INC-100000);
True: Tax = 0
]
21
Conditional statements
• Case statement in lieu of nested if-then-else statements
If C1 then S1 else [if C2 then S2 else [if C3 then S3
else [if C4 then S4 else [] ]]
case [ case [
INC > 100000: T = 0000 + 0.10*(INC-100000);
<C1>: S1;
INC > 200000: T = 10000 + 0.20*(INC-200000);
<C2>: S2; INC > 300000: T = 30000 + 0.30*(INC-300000);
<C3>: S3; True: T = 0
]
<C4>: S4
]
case [
INC > 300000: Tax = 30000 + 0.30*(INC-300000);
INC > 200000: Tax = 10000 + 0.20*(INC-200000);
This algorithm will give
INC > 100000: Tax = 0000 + 0.10*(INC-100000);
completely incorrect
True: Tax = 0 results.
] Why? 22
Input/output operations
• Input operation
• Effectively a read followed by an assignment
• Read from keyboard
Example:
input (x1, x2, L, H, W, Name)
• Output operation
Effectively a write to computer screen
Example:
output (‘The volume is:’, H*L*W)
23
Iteration statements using while-do
• Iteration using while C do S
where C is a condition, and S is a statement
• Example:
# Find the largest integer n > 0 such that 2**n < 1000
n = 1; x = 2**n;
while x < 1000 do [n = n+1; x = 2**n]
output(‘largest n>0 such that 2**n < 1000”’, n-1)
because the last x=1024 and n=10, but it does not satisfy the
while loop, hence output is 'n-1'
True
C S
False
24
Iteration statements using while-do
• Iteration using while C do S
where C is a condition, and S is a statement
• Another example:
# Compute the sum of n numbers
input(n);
if n > 0
then [k = 1; sum = 0;
while k ≤ n do [input(x); sum = sum + x; k = k +1];
output(‘the total is’, sum)
]
else output(‘no numbers to add’)
25
Iteration statements using while-do
• Iteration using while C do S
or using the repeat S until C
True
C S
• Note:
while C do S False
is the same as:
if C then repeat S until not C
• And S
repeat S until C
is the same as: False
C
S; while not C do S
True
26
Iteration statements using for loop
• Iteration using for k in <sequence> do S
• <sequence> is an ordered set of objects, typically integers
• Example:
# Compute the dot-product of two vectors U and V
# vector U = [u(1), u(2), …, u(n)], n>0
# vector V = [v(1), v(2), …, v(n)], n>0
input(n);
for i in <1, 2, …, n> [input(u(i))];
for j in <1, 2, …, n> [input(v(j))];
sum = 0;
for k in <1, 2, …, n> [sum = sum + u(k)*v(k)];
output(‘Dot product U*V of vectors U and V is ‘, sum)
27
Iteration statements using for loop
• Iteration using for k in <sequence> do S
• <sequence> is an ordered set of objects, typically integers
28
Q&A
• On algorithms
• On Python programs
• On testing
• On debugging
• On documentation
• On scalar data items
• On structured data
• On representation of scalar data
29