Topic 1 Introduction To Programming Revision 202201
Topic 1 Introduction To Programming Revision 202201
TOPIC 1 CLO1
INTRODUCTION TO PROGRAMMING Solve programming problems by applying concepts
of problem-solving and structured programming.
CLO2
Develop computer programs by using various
structures with relevant algorithms and functions.
References: 2
1. Harbour, J. S. (2012). More Python programming for the absolute beginner. Boston, MA: Course
Technology/Cengage Learning
2. Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
3 4
5 6
1
1 DEFINE THE PROBLEM (CONT’D) 2 DEVELOP THE ALGORITHM
Example: Shop A is offering a 20% discount for all their The solution defined in Step 1 is expanded into an
products. Calculate the payment for each customer algorithm.
for his or her total purchase.
Input – total_purchase A program must be systematically and properly
Process: designed before coding begins.
Input total_purchase
Calculate payment
Display payment This design process results in the construction of an
Output - payment algorithm.
7 8
9 10
Process Flow
Pre-defined
Input / Output process
Decision Off-page
11 12
connector
2
2 DEVELOP THE ALGORITHM (Cont’d) 3 TEST THE ALGORITHM FOR CORRECTNESS
This step is one of the most important in the
Flowchart development of a program, and yet it is the step most
Start
often forgotten.
discount_rate = 0.20
The main purpose of desk checking (testing the
algorithm) is to identify major logic errors early, so that
INPUT total_purchase
they may be easily corrected.
payment = total_purchase *
(1 – discount_rate) Early error identification is better because it will save
time and cost.
PRINT payment
13 14
End
5 RUN THE PROGRAM ON THE COMPUTER 5. RUN THE PROGRAM ON THE COMPUTER (Cont’d)
This step uses a program compiler and programmer- Logic errors
designed test data to machine test the code for syntax These errors are related to logic of the program execution.
errors and logic errors. Logic errors occur when the code does not run in the way it has
been intended because of the way it is written.
Syntax errors are any violation of rules of the language Logic errors can be easy-to-spot blatant mistakes or subtle
results in syntax error. problems that are extremely difficult to spot.
E.g. A Python print statement for Python version 3.x E.g. Wrong operations in a mathematical formula to calculate
✘primt “Game over” ✘ print "Game over length of hypotenuse of a right-angle triangle.
✘ Print “Game over” ✔ print ("Game over") ✘ h = math.sqrt(a*a – b*b)
✔ h = math.sqrt(a*a + b*b)
Syntax: all programming languages have a set of rules on
what is, and is not allowed when forming a line of code. In
other words, all programming languages have a ‘grammar’.
17 18
3
6 DOCUMENT AND MAINTAIN THE PROGRAM Internal & External Documentation
19 20
21 22
Sample Code:
>>> thisnumber=15 Numbers Sequence
>>> id(thisnumber)
140716303246608 Mutable Immutable
>>> type(thisnumber)
<class 'int'>
>>> thisnumber Real
Lists Tuples
Integral Strings
15 (float)
23 Integers Booleans 24
https://docs.python.org/3/reference/datamodel.html#index-0 (int) (bool)
https://docs.python.org/3/library/stdtypes.html
4
TYPE: NUMBERS TYPE: IMMUTABLE (STRINGS)
Numbers: Integers Strings
You can use an integer to represent numeric data, and Strings are collections of alphabets, words or other
more specifically, whole numbers from negative infinity to characters. In Python, you can create strings by enclosing a
infinity, like 4, 5, or -1.
sequence of characters within a pair of single or double
quotes. For example: 'cake', "cookie", etc.
Numbers: Boolean
This built-in data type takes up the value of True or False
0, [ ], { }, ( ) evaluates to False
-1, 1, 50, 50.1, [1,2] evaluates to True
Float
"Float" stands for 'floating point number'. You can use it
for rational numbers, usually ending with a decimal figure, 25 26
such as 1.11 or 3.14.
29 30
5
TYPE: MUTABLE (LISTS) TYPE: MUTABLE (LISTS)
SLICING LIST SLICING
Slice is a sublist / substring from index start to index Example:
>>> list1 = [2, 3, 5, 7, 9, 1, 33, 21]
end – 1. >>> list1[2 : 4]
Syntax : List [start : end] [5, 7]
>>> list1[ : 4]
[2, 3, 5, 7]
>>> list1[3 : ]
[7, 9, 1, 33, 21]
33 34
Reference: Harbour, J. S. (2012). More Python programming for the absolute beginner. Boston, MA: Reference: Harbour, J. S. (2012). More Python programming for the absolute beginner. Boston, MA:
Course Technology/Cengage Learning Course Technology/Cengage Learning
EXAMPLE PROGRAM WITH CONSTANTS AND VARIABLES 1.4 VARIABLE NAMING CONVENTION
# Assign a value to radius Rules to follow to create legal variable names:
radius = 20 1. A variable name must consist of only letters, digits or
underscores.
# Compute area
PI = 3.14159 # 3.14159 is a constant 2. A variable name cannot contain spaces.
area = radius * radius * PI
3. A variable name must start with a letter or an
underscore. It cannot start with a digit.
# Display results
print("The area for the circle of radius",
radius, "is", area)
35 36
Reference: Harbour, J. S. (2012). More Python programming for the absolute beginner. Boston, MA:
Course Technology/Cengage Learning Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
6
1.4 VARIABLE NAMING CONVENTION (Cont’d) 1.4 VARIABLE NAMING CONVENTION (Cont’d)
4. An identifier cannot be a keyword. Keywords, also Some standards to follow to create good variable names:
called reserved words, have special meanings in Python. 1. Choose descriptive names.
[Refer Appendix 1] - Use score instead of s as the variable name as score
is more meaningful.
5. Uppercase and lowercase characters are distinct. This
means the variable name ItemsOrdered is not the 2. Be consistent.
same as itemsordered.
- Choose either averageMark or average_Mark
and stick to one style all the way.
37 38
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
39 40
Reference: Harbour, J. S. (2012). More Python programming for the absolute beginner. Boston, MA:
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson Course Technology/Cengage Learning
41 42
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
7
AUGMENTED ASSIGNMENT OPERATORS COMPARISON OPERATORS
The operators +, -, *, /, //, % and ** can be Operator Meaning Example Condition Result
combined with the assignment operator (=) to form (radius = 5)
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
The logical operators not, and, and or can be used to +x, -x, ~x Positive, negative, bitwise NOT
create a composite condition. *, /, //, % Multiplication, division, integer division, and
Operator Description remainder
not logical negation +, - Addition and subtraction
and logical conjunction <, <=, >, >=, ==, !=, is, is Comparison, identity, membership operators
not, in, not in
or logical disjunction
not x Boolean NOT
Logical operators can be used to combine these and Boolean AND
conditions to form a compound expression. or Boolean OR
=, +=, -=, *=, /=, //=, %= Assignment
45 Associativity: What about priority within the same level? 46
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson https://docs.python.org/2/reference/expressions.html#id25
47 48
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
8
1.5.2 OPERATORS (FOR MUTABLE AND IMMUTABLE
OPERATOR OR SEQUENCES)
p1 p2 p1 or p2 Example Conditions
(p1: age = 24, p2: gender = ‘F’) The +, *, and in/not in Operators
False False False (age < 18) or (gender == ‘M’) is False,
because (age < 18) and (gender == ‘M’)
are both False
Operator Description
False True True (age < 18) or (gender == ‘F’) is True, + Concatenate two lists/string
because one of the conditions, (gender == ‘F’)
is True
* Replicate elements in a list/string
True False True (age > 18) or (gender == ‘M’) is True, in Return True if element is in a list/string
because one of the conditions, (age > 18) is
True
not in Return True if element is not in a
True True True (age > 18) or (gender == ‘F’) is True,
list/string
because (age > 18) and (gender == ‘F’)
are both True
49 50
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
9
1.6 EXPRESSIONS AND EQUATIONS (Cont’d) PYTHON EXPRESSIONS
Example assignment statements: Example arithmetic expression:
x = 5
3 + 4𝑥 10 𝑦 − 5 𝑎 + 𝑏 + 𝑐 4 9+𝑥
y = x + 1 − + 9( + )
5 𝑥 𝑥 𝑦
• Explanation:
which can be translated into a Python expression as:
5 is assigned to x.
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
The result of x + 1 is assigned to y after the
statements are executed. Thus, 6 is assigned to y.
55 56
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson Reference: Liang, Y.D. (2013). Introduction to Programming using Python. Boston: Pearson
10
APPENDIX 2: EXAMPLE PROGRAMS
Program 1: Calculating area of a circle.
Output:
61
11