EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Lab 2 – Introduction Python
2020
Resource: Instructors:
Python Crash Course by Matthes Dr Lim Tiong Hoo
2A43
[email protected]
1
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Intended Learning Outcome
Describe the development of the computer and the basic
principles of computer systems, networks and applications.
Use the binary, octal and hexadecimal number systems and
converts between number bases.
Write simple assembly language programs in the Z80 or
similar language.
Write simple high level programs using the C
programming language or equivalent
Apply network concepts to connect a computer to the UTB
network and uses network resources.
2
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
What is computer Program?
Internet Definition
computer program ‐ a sequence of instructions that a computer
can interpret and execute. (The Free Dictionary)
A computer program is a collection of instructions that performs a
specific task when executed by a computer. (Wikipedia)
What is the difference between sequence and collection?
Which of the two definitions do you think is better and
why?]
3
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
How a program runs
Source code – a computer program you write in english
A compiler - Reads your source code and produces output
file containing the machine code
Machine code - is given as input to an operating system
which executes the instructions in the file depending on
the instruction set
Interpreters - Many of the more recent languages such as
Python use a program called an Interpreter which
effectively combine the compile step and the execution
step together.
REPL is an acronym for Read, Execute, Print, Loop is
programming development environment which is provide
by some interpreted languages, like Python.
4
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
How to write your program - Defining
the problem
A clear definition of the problem to solve or the task to
perform.
Example boiling an egg
Most people will choose to write the instructions as a set
of bullet points with an implying an order of execution.
When designing a code, there are two techniques that
you can employ
Writing the pseudo‐code
Drawing the flowchart
5
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Algorithm, Pseudocode and Flowchart
An algorithm is a step‐by‐step set of instructions required
to complete a given task.
Larger more complex program may use several algorithms
to complete a task, each algorithm being used for some
sub‐task within it.
Pseudocode is a made‐up informal language that enables
programmers to write down their solution processes in an
algorithmic type way.
Pseudocode is a "text‐based" detail (algorithmic) design
tool.
Flow charts is a diagrammatic way of representing your
algorithm or program.
Simple shapes are used to represent the various elements
(lines of code) in your program. 6
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Testing Your Program
Testing is a planned process
Aims - to discover if anything is wrong with the program
code.
Possible problem - the programming logic for the
algorithm was incorrect.
Debugging is the process of detecting and correction the
errors in the programming logic.
All computer programs should be comprehensively tested.
The answer to the question
When have we done enough testing?
Answer : when you have either run out of money or run out of time
7
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Testing Strategy
Have a plan! – start it early!
Construct a test plan as soon as the program specification is available
Know what you are testing for
Ensure that the program behaves as expected with all possible inputs
and values for the variables
Ensure that sufficient test cases is exercised in every path or part of a
path through the program code
Know what results you expect from any given set of test
conditions
Record your test plan and results
VVT – Verification, Validation and Testing
Validation - Have we written the right program? Does it match the
specification we were given?
Verification - Have we written the program right? Does the
programming logic we have used result in getting the right answers 8
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
About Python
High Level Programming Language
Interpreted
Support 3 main programming style
Imperative/procedure
Object-oriented
Functional
Free
Platform independent
Version 3.X
9
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Installation, Run and Help
Download & Install Python – www.python.org
Download and Online documentation
www.python.org
Run Python
C:\Users\ITB35\AppData\Local\Programs\Python\Python36-
32\python.exe
Install a python text editor - Geany
https://www.geany.org/download/releases/
Build->SetBuildCommands: Execute Command
Execute -
C:\Users\ITB35\AppData\Local\Programs\Python\Python36-
32\python "%f"
Help()
10
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Coding
Must follow python syntax and style
Focus on readability
Indentation: use 4 space
11
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Supported Module/Library
Python comes with many useful modules
Modules are accessed using import
import sys, os # imports two modules
Modules can have subsets of functions
os.path is a subset within os
Modules are then addressed by modulename.function()
sys.argv # list of arguments
filename = os.path.splitext("points.txt")
Dir(math) math.sqrt(), math.pi,
Usage – import math / import math as m
12
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Data Types, Variables and Constants
Strings
Integers
Floats
Characters
type() – returns the type of variable
13
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Input, Print and comments
print(a) - prints output, by default to the screen.
input() - allows the user to type some input at the
keyboard followed by a return key
Example
inputname = input(“What is your Name? ”)
# - Comments
14
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
More than printing or displaying
Python is an object oriented language
Practically everything can be treated as an object
Try to print “hello world” as a string
Strings, as objects, have methods that return the result of
a function on the string
15
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
String Manipulation
In this case “mytext = “hello world”
mytext.title()
mytext.upper()
mytext.isdigit()
mytext.islower()
16
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Python Objects
Lists (mutable sets of strings)
var = [] # create list
var = [‘one’, 2, ‘three’, ‘banana’]
Tuples (immutable sets)
var = (‘one’, 2, ‘three’, ‘banana’)
Dictionaries (associative arrays or ‘hashes’)
var = {} # create dictionary
var = {‘lat’: 40.20547, ‘lon’: -74.76322}
var[‘lat’] = 40.2054
Each has its own set of methods
17
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Branching and Control
The colon ‘:’ at the end of the line
The indentation
End by removing the indent.
Conditional branch
If condition_is_true : (example a==b)
Run code
else :
Do this (optional and if more, use elif)
Control Loop
for is used when the number of iterations is known in advance
for is great for manipulating lists
for x in [1,2,3]:
print(x)
while - loop while the condition is true
while x <= 10:
x=x+1
break commonly used with while True:
18
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Reading and Writing to a file
The basic function to work with a file is open()
Requires two parameters
the name of the file you wish to open
Functions to perform (‘r’ = read, ‘w’ = write, ‘a’ = append)
Examples:
infile = (‘\directory\moredirectory\filename.csv’)
fread = open(infile,’r’)
fwrite = open(infile,’w’)
fread.close()
fwrite.close()
19
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Python Programming Assignment
A file which contains a table representing the final medals
table from the 2016 Rio Olympic games is available for
download from the moodle. The table is in final position
order and includes the number of Gold, Silver and Bronze
medals won by each team
Your task
Write a program that will accept as input two Country codes (as
specified in the Country column) and produce an output with the
number of additional medals the second country would have needed
to beat the first country.
All combinations of additional medals should be found.
20
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
Submission (4 weeks to complete)
A program
A report containing
the program specification / description,
pseudo‐code and / or flowcharts for your algorithm,
A code file containing the code of the program
Including a suitable block comment at the beginning
Any appropriate in‐line comments
A test plan
an example of expected output
with test data
the test results
An example run of the program
Indicating the input used
and the output produced.
21