2021 PSP Week 3 Introduction Programming Lecture New
2021 PSP Week 3 Introduction Programming Lecture New
• Programming Language
• How does programs run ?
• Python
• From Problem Solving to a Program
• Strings
What is Programming?
• Programming is not just about learning a
programming language and coding
• Writing a program is only one part of a whole
software engineering process
• Programming requires you to :
1. Understand the problem and what we want to
achieve
2. Come up with a solution to the problem
3. Write the program to implement the design
4. Test the program to see if it works up to specification
• We practice 1 and 2, let’s focus on step 3
http://www.automatedtrader.net/Files/news/periodic-table.jpg
Compiling, Interpreting, and
Running Programs
When we program, the code that we write is known as
source code. How do we get from that to …
Source Machine
Code Code
Compiling, Interpreting, and
Running Programs
• There are two basic ways of doing this:
• Code can be interpreted.
Each line of source code is read in turn, and then
converted into machine code instructions, which are
then executed.You need to have access to the source
code every time you run it.
• Code can be compiled.
The entirety of the source code is read and converted
into a separate machine code program which you can
run whenever you like.
In theory, once the program is compiled, you could
throw the source code away and still execute the
program (in practice this would be a very bad idea!).
Understanding What Happens
when a Program Runs
• When we are trying to understand what happens when
a program runs, we will “pretend” that the source code
is being directly executed by the machine.
• We need to know about compilation and interpretation
in order to understand how to prepare a program for
execution, and deliver it to a potential user.
• Trace tables are useful to understand the code
n1 n2 Output
30
30 20
30 20 50
What we need in a
programming language
• In Python
name = input("enter your name: ")
Python: count = 1
while count < 10:
print(count)
count = count + 1
Iteration in Python
Bug:
isAnagram is
always true
Top Level Design of the
ANAGRAM problem
Input number
Set factor to 2
Set factor_printed to False
While factor is less than 10
if number is an anagram then
set factor_printed to True
print factor
increment factor
If not factor_printed then
print no
Designing the Sub-Problem
• IsAnagram receives the number and the factor
• Return True if the number is an anagram, False otherwise
• But be careful…
Subscripts
• In many languages including python we can
address a single element of a string e.g.
a = "hello"
a[0] "h"
a[4] "o"
A = "hello"
A[1:3] "el"
total = 0
for every digit in ISBN
if digit is a number
total = total+ digit*position
else
total = total + 10
return total
Python program
def calculateTotal(ISBN):
total = 0;
for pos in range(-10,0):
if ISBN[pos]>='0' and ISBN[pos] <= '9':
total = total + int(ISBN[pos])* -pos
else:
total += 10
return total
Trace Table
Summary
• Programming in Python
• How to move from design to implementation
• Learning the syntax of the language
Homework
• Review week 3 lecture and complete the exercises
• Preparation for week 3 quiz (5 marks)
• Time&Location: Week 3, seminar 2
• Time limit: 20mins
• Questions?
Thank You!