Section 8 - Program Implementation-Py
Section 8 - Program Implementation-Py
IMPLEMENTATION
2
Computer Programs
Low-Level Language
High-Level Language
Example :
These languages are machine
independent. That is, programs
written on one computer can Third generation language
generally be used on another Fourth generation language
similar computer. They also use Fifth generation language
keywords similar to English and
are easier to write.
8
Third Generation Languages 3GL
Translating Code
Machine code is the lowest level of programming language because the instructions are
executed directly by a computer’s central processing unit (CPU). It is important to
understand that every CPU or CPU family has its own machine code instruction set.
Machine code instructions are simply numbers stored as a binary pattern of
bits i.e., 1010100101000000.
Because machine code instructions are the only ones the CPU can execute, the source code
for ALL other programming languages must be converted into machine code before it can
be executed. This translation is carried out by special programs
called compilers, translators/interpreters or assemblers.
14
Types of Translators
Interpreter
Assembler
A compiler is a translator used to convert
high-level programming language to low-
level programming language. It converts
the whole program in one session and
Compiler reports errors detected after the
conversion. The compiler takes time to
do its work as it translates high-level
code to lower-level code all at once and
then saves it to memory.
15
Just like a compiler, is a translator used
to convert high-level programming
language to low-level programming
language. It converts the program one
line at a time and reports errors
Interpreter detected at once while doing the
conversion. With this, it is easier to
detect errors than in a compiler. An
interpreter is faster than a compiler as it
immediately executes the code upon
reading the code.
16
An assembler is a translator used to
translate assembly language to
machine language. It is like a
compiler for the assembly language.
An assembler translates a low-level
language, an assembly language to an
even lower-level language, which is
Assembler the machine code. The machine code
can be directly understood by the
CPU.
READ:
https://teachcomputerscience.com/pro
gramming-languages/
17
18
There are several steps that must be completed before a program can be put in
use by a user.
Source Code Writing
The procedures must then be converted into the syntax of a suitable programming
language. This “typed up” version of the code or procedures is known as the SOURCE
CODE.
Compilation
the compiler scans the source code for syntax errors. If there is no error, the code is
made into an executable file. The result of this phase is called OBJECT CODE.
Linking
Linking is a process that takes place when
more than one object code is needed to
create an executable code. It creates a link
among object codes after the completion of
the compilation phase.
Execution
The Steps of In the execution phase, the program is
Programming seen in a console (e.g., DOS Window) or as
an application.
Implementatio Maintenance
n A program or application sometimes
requires maintenance after it is placed into
production (in use). In this phase, sections
(modules) in the program may be deleted,
corrected or added to please the users’
needs.
20
21
Errors!
When writing and running code,
things happen!
A programming error is a fault
that occurs during program
compilation and or execution.
These errors either cause
programs to crash (stop) or
produce the incorrect result(s).
Syntax Errors - Errors in the use of the
language.
Every programming language has its own
rules, just like English or Spanish.
Syntax errors syntax errors are small
grammatical mistakes, sometimes limited to
a single character.
The compiler will complain about syntax
errors, the programmer must fix these errors
before the program can be compiled and ran.
22
Logic Errors - is a mistake in a
program's source code that results in
incorrect or unexpected behavior.
Logic errors occur when simple errors
are made by a programmer; the flow
or sequence of the program leads to
Types of Errors incorrect results.
Most logic errors occur in
mathematical operations where
incorrect symbols are used in
calculations.
23
Run - Time Errors
As the name suggests, runtime errors
occur while a program is being
executed. These errors are sometimes
cause by the input received from the
user, lack of memory space on the
computer or a popular calculation of
trying to divide a number by zero.
These errors are the most difficult
Types of Errors errors to trace because sometimes,
the exact cause may not be readily
visible.
A type of runtime error is a memory
leak. This type of error causes a
program to continually use up more
RAM while the program is running. A
memory leak may be due to an infinite
loop.
24
There are two ways in which a
programmer can locate errors within
his or her programs.
Finding and These are by using:
Fixing Errors Debugging Techniques
Testing Techniques
25
Debugging a program is a three-step
process:
Detection / Error Locating -finding
where the errors are located within
the source code
Debugging
Diagnosis - determine the cause of
Techniques the error
Correction
26
Several tools and techniques can be
used to carry out the debugging
process.
Debuggers – tools used to assist
programmers in locating errors in a
program.
Debugging Traces – Printout of the step-by-
step flow of the program.
Techniques
Variable Checks – Displays the
value of variables at different
points within the program.
Step Mode – The program is
executed line by line until the error
occurs.
27
28
29
“Dry Run” is the term used to describe
the checking of a program for errors
without the use of a computer or
compiler.
Dry run testing is a technique used by
Dry Run many programmers. This technique
involves the use of trace tables,
Testing algorithms and pseudo-codes. The
programmer “runs” through the
program, jotting the results down on
paper. This is done before any coding
starts (with a programming language).
30
31
Debugging errors.
vs Testing
Often testing leads to runtime errors
which leads to debugging.
32
Programmin
g
PYTHON
Programming in Python 35
36
Documentation
Must have Developer Name, Program
Name, Description and Date.
Put this information in a comment section
at the beginning of the code.
#Keisha Morrison, Hello World, This
program displays “Hello World”, 6/9/2023
Initialize Variables
Python Syntax List all the veriables to be used with
Basics sample data to initialize it.
Add a comment above with the Data Type
#Strings
Name = “name”
Use comments to separate each section
of code and add labels.
#######While loop for accepting
data##########
37
38
39
40
Python Functions
All user input gets accepted as string/text. The following are different data types
Even if the user types the number 4 the Integer == Whole number int
program sees it as “4”. You cannot add “4” to Real (Float) == Decimals float
5. They are of different data types.
String == multiple letters or number str
So how do we get numbers from users? Type
casting!
Boolean == true or false == bool
E.g.:
If you want to specify the data type of a
int(“4”) converts the string “4” to the integer 4. variable, this can be done with casting.
float(“4”) converts the string “4” to the decimal
4.0
44
Assignment Statements Math Operators
The operator is replaced by =
Python follows BOMDAS!
E.g., num = 5
Conditional IF STATEMENTS
Statements
47
Python
Logical
Comparisons
48
Simple If Statements
You can test more than one condition within the same if statement.
a = 200
b = 33
if b > a:
print("b is greater than a") The else keyword is used
elif a == b: when all the above
print("a and b are equal") conditions fail.
else:
print("a is greater than b")
The elif keyword is used Note the indentation and the colons.
when the previous condition
is false, and you want test
another condition.
50
Nested Ifs
20 Marks
Test 54
You are designing a simple grading system for a school. Each student's
final grade is determined based on their scores in three subjects: Math,
English, and Science. To pass, students must meet the following criteria:
The average score in all subjects must be 70 or higher.
No subject score should be below 50.
Write a Python program that takes input for a student's scores in Math,
English, and Science, calculates their average score, and then
determines whether the student has passed or failed.
Note: You can assume that all input scores are integers, and there's no
need to validate input in this exercise.
20marks
55
WHILE LOOP
Repetition FOR LOOP
56
While Lo0ps
Pseudocode Python
FOR i = 0 to 4 DO for i in range(5): # Iterating through 0 to 4
PRINT i print(i)
Write a python program to read the names and ages of ten (10) people
and print the name of the oldest person.
There is no need to store all 10 names or ages. You only need to
store/print the oldest persons name and age.
60
Question Lo0ps
61
Classwork
Write a Python program to read ELEVEN numbers find their average and
print it. The algorithm should also print the number of times 6 occurs in
the data.
62
SBA Time