Lab 2 - Introduction To Python Programming PDF
Lab 2 - Introduction To Python Programming PDF
Resource: Instructors:
Python Crash Course by Matthes Dr Lim Tiong Hoo
2A43
lim.tiong.hoo@utb.edu.bn
1
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
2
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
3
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
5
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
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
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
13
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
# - Comments
14
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
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
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
fread.close()
fwrite.close()
19
EE1101 – Principles of Computer Systems Universiti Teknologi Brunei
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