0% found this document useful (0 votes)
12 views

Csc201 Lecturenote2023 24 One

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Csc201 Lecturenote2023 24 One

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

O.BÁFÉ.MI AWÓLÓ.WÒ.

UNIVERSITY
ILÉ-IFÈ., NIGERIA.
FACULTY OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INTRODUCTORY LECTURE NOTES

CSC 201: Computer Programming I


3 Units
Harmattan Semester, 2023-2024 Session
THIS DOCUMENT IS NOT FOR SALE !

O
. DÉ.JO
. BÍ, O
. dé.túnjı́ A.
Room 109, Computer Buildings,
oodejobi@oauife.edu.ng

November, 2024
Contents

1 Introduction to Computer programming 1


1.1 The mental activity of Ìs.irò (Computing) . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 TASK 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Agency of computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.1 Human language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.2 Distinctions between Human and Programming languages . . . . . . . . . . . . 5
1.2.3 Language and computing process . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.4 The tools of language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2.5 Examples of Polarity and Logic expressed through structure . . . . . . . . . . . 8
1.3 What is “A computer”? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4 History of Modern Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.4.1 TASK 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.5 The Computer System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.6 Computer hardware configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.6.1 Input-Output devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.6.2 Central Processing Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.6.3 Memory devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.6.4 Memory metric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.7 Computer Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.7.1 System software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.7.2 Application software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.8 What is Computer Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.8.1 Operand: Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.8.2 Operation: Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.9 Foundation of Computer Programming Language . . . . . . . . . . . . . . . . . . . . . 24
1.9.1 Familiarly with Programming Language . . . . . . . . . . . . . . . . . . . . . . 25
1.9.2 Python Instruction format and structure . . . . . . . . . . . . . . . . . . . . . . 25
1.9.3 Python Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.9.4 Python Reserved words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.9.5 Python Variable and Constant identifiers . . . . . . . . . . . . . . . . . . . . . . 26
1.9.6 Python Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.9.7 Familiarly with Programming Process . . . . . . . . . . . . . . . . . . . . . . . 28
1.10 Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.11 Computer program development process . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.11.1 TASK 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.12 Case example: FindAverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.13 TASK 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
1.14 TASK 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.15 Appendix A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

ii
List of Tables

1.1 Evolution of Computer technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11


1.2 Computer memory metric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.3 Python Reserved or Key words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.4 Python Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.5 Python code for the Flowchart in Figure 1.14 . . . . . . . . . . . . . . . . . . . . . . . 34
1.6 Python code for the Flowchart in Figure 1.15 . . . . . . . . . . . . . . . . . . . . . . . 35
1.7 Python code for flexible Average computation . . . . . . . . . . . . . . . . . . . . . . . 35
1.8 Another Python code for flexible Average computation . . . . . . . . . . . . . . . . . . 36

iii
List of Figures

1.1 Human communicating with Self (using L0 ) and with Others using habitual language
(LH ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Human instructing machine using Programming language (Lp ) . . . . . . . . . . . . . . 4
1.3 Human communicating through machine using Instrument of Language (LI ) . . . . . . . 4
1.4 Information communicated with human language . . . . . . . . . . . . . . . . . . . . . 5
1.5 Tools of human language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.6 Definition of Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.7 Ayo an example manual computing tool used in game playing . . . . . . . . . . . . . . 10
1.8 Plate of IBM 370 machine at the University of Ife. Computer Centre . . . . . . . . . . . 12
1.9 Evolution of Mobile computing and communication devices . . . . . . . . . . . . . . . 13
1.10 Date stamp history of Computer programming language . . . . . . . . . . . . . . . . . . 14
1.11 Structure of a Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.12 Fundamentals of Data and Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.13 Flowchart Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
1.14 Design for the Average Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.15 Design for the Average Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.16 Python Programming language operators . . . . . . . . . . . . . . . . . . . . . . . . . . 38
1.17 Precedence of operator in Python programming language . . . . . . . . . . . . . . . . . 38

iv
CSC201: LECTURE NOTE 1

Introduction to Computer programming

The Lectures in this aspect of the course is concerned with foundation and fundamental
principles of Computer Programming. The abstraction and definitions of the terms
(words) used in computer programming will be discussed. This is with the view to
exposing students to language and process of modern computing. Familiarity with
computing terms and how the terms are used to construct valid expression is essential
to the effective understanding of this course.
We will also discuss the history of modern computers as well as the evolution of
the languages used to program them.
The process of computer program development will also be explained and demon-
strated with examples using the Python programming language.

1.1 The mental activity of Ìs.irò (Computing)


Èrò (Mental activity) is innate to individual human. Èrò (Mental activity) is con-
ducted with Human language. Instances of state in human mental activity find ex-
pression through an Instrument of language. Humans inform each other by sharing
state of their individual mental activity through an Habitual Instrument of Human
Language.
The mental activity of computing is reduced into a process to facilitate its lan-
guage expression. A process comprises differentiable instances of state. The Àlàfo
(Interval) between a pair of state is called ı̀s.ı́ (a transition). Therefore, a process can be
viewed as comprising instances of state and transition.

Process = state + transition

Modern humans have created special languages, such as Mathematics, for giving
expression to their mental activity. Languages have also being created for instructing
machines to carry out a formally prescribed process. The languages for instructing
computing machines are called Computer programming language. A computer pro-
gramming language is used to instruct the machine on:

1
(i.) What action to perform (Operation or Function); (This corresponds to transi-
tion)
(ii.) The identity of the items on which the action in (i.) above will be performed
(Data or Operand) (This corresponds to state)

Therefore, a computing instruction comprises Operation and Operand.

Instruction = Operation and Operand

A computing process is executed by a computing agency.

1.1.1 TASK 1
Get and install Python environment into your MOBILE PHONE.
If you are using Android based Phone Install Python Android (Pydroid 3)
from Google Playstore into your mobile phone or other mobile device.
If you are using Apple based Phone Install Python Coding IDE from Apple
Store into your mobile phone or other mobile device.

1.2 Agency of computing


Ìs.irò (Computing) is a process (Ìlànò.n). The Yorùbá word Ìs.irò (Computing) translates
into English as (Ì-s.ı́-èrò): “The act of giving expression to instances of state in human
mental activity”. It also transcribes as: Ìs.ı́+Èrò (“The transition in mental activity”).
Ìlànò.n (A process) is the language rendering of the state and transition ascribed to hu-
man mental activity. Therefore, a process comprises Two (2) aspects: (i) State and (ii)
Transition. The instances of state and transition in a process are expressed through an
instrument of language. Therefore, a process is the aspect of human mental activity
prescribed and expressed using a language.
Humans are biological agencies. The computer is a machine. A machine is a ma-
terial agency. The computing machine is a tool for material rendering of an efficiently
and precisely expressed process. The Computer programmer is a human or group of
humans that instruct the computing machine on how to carry out a process. There are
Two (2) fundamental differences between biological (e.g. humans) and material (e.g.
computer machine) agencies:

(i.) Faculty of language and


(ii.) Sensory organs.

“Sensory organs” and “Faculty of language” are inherent in biological agencies.


There is neither sensory organ nor faculty of language in a material agency. As depicted
in Figure 1.1 an individual human communicates with himself/herself through his/her

2
own faculty of language (Èdè-orı́). However, an individual can share his/her own men-
tal state with other humans using an habitual instrument of language (Ìpèdè). Such
language includes: (i) Yorùbá, (ii) Hausa, (iii) Igbo, (iv) Tiv, (v) English and so forth.
Humans also use specialised language such as Mathematics to communicate amongst
each other.

LH
A B
LH

L0 L0

Figure 1.1: Human communicating with Self (using L0 ) and with Others using habitual language (LH )

A material agency (machine) is created by human as a tool for achieving a pre-


scribed task or process. The purpose of material agency (machines) is determined by
its human designer and/or users. Human also create specialised instrument of language
to instruct a machine. As depicted in Figure 1.2, the programming language Lp is used
by humans to instruct computing machines. The language LI is used when human
communicate with each other through a computing machine.
It is also the case that humans can communicate with each other by using a com-
puting machine as depicted in Figure 1.3. This is called computer mediated communi-
cation.

3
Lp
A M
Lp

L0 L0

Figure 1.2: Human instructing machine using Programming language (Lp )

LI LI
A M B
LI LI

L0
L0

Figure 1.3: Human communicating through machine using Instrument of Language (LI )

1.2.1 Human language


Humans use their habitual instruments of language to render, formulate and give mate-
rial expression to instances of mental state.
An instrument of language is used to “give expression to” and/or “elicit Às.amò.
(Information) from”:

(a.) Ò
. rò. (Speech)
(b.) Ìs.e (Action)
(c.) Àrokò (Message)

Às.amò. (A piece of information) can connote:

4
(i.) An explanation.
(ii.) A piece of advice,
(iii.) A warning and
(iv.) An instruction,

Às.amò. (information) will be Àgan (ineffable) in the absence of an instrument of


language. The information in a message is accessible to the habitual users of the lan-
guage in which it is expressed.

òrò d
Speech
Sọ te
ula
tic
Ṣe Ar
Àṣamò Ìṣe Expressed
Action
Pa Infromation Sen
d

Àrokò Message

Figure 1.4: Information communicated with human language

For example, different messages will be elicited from the expression Y = A + B


by :

(i.) A Mathematician,
(ii.) A Physics,
(iii.) A Genetics
(iv.) In Computing .

1.2.2 Distinctions between Human and Programming languages


The operation of a Computing machine is to the extent of the instructions written by its
human programmer. The instruction to achieve a problem-solving process is expressed
using a “Computer programming language”. “Computer programming language” dif-
fers from human written language in the following aspects:

(i.) “Computer programming languages are prescribed and specifically designed to


communicate with machines. The expression of Computer instructions may NOT
conform to the grammar of Witten human language, including that of Mathemat-
ical. For example, (a) i = i + 1 , (b) i + + , and (c) i = + are valid computer
instruction. But they do NOT conform to the grammar of human written lan-
guage.

5
(ii.) Computer machines do NOT learn “A programming language” before they ex-
ecute its instructions. Human must learn a language before they can use it to
communicate or effect action.
(iii.) Computer machines do NOT process the symbols in the instruction of “Program-
ming languages”; they process a material rendering of the instruction. A com-
puter manipulates prescribed signals rendered to the extent that technology per-
mits. Such technology include Mechanical, Electrical (through Diode Valve and
Transistors), Electro-magnetic, Electronic, Micro-electronic, and Nano-electronics.
Symbols are directly manipulated by humans when processing an expression.
(iv.) The capacity to determine whether there is a mistake in an instruction or program
TRANSCENDS a Computer machine. However, a computer can be programmed
to report errors in the invalid expression of an instruction.

In this course, therefore, you should NEVER confuse a material agency (ma-
chine), no matter its sophistication, with biological agency (human).

1.2.3 Language and computing process


Fundamentally, computing is human mental activity. The process ascribed to aspects
of the human mental activity of computing is expressible through a Language of sym-
bols. The process expressed through a language of symbols can further be reduced into
a Computer Programming Language. The instructions in a programming language are
used to influence the state and transition of computer hardware components. A com-
puter program is a sequence of instructions that describe a process from its “Beginning
to its “End.
“Programming Language are created by mimicking the Written human language.
For example, the OPERAND (OR DATA) in computer instruction mimics the NOUN
in written human language. Also, the OPERATOR in computer instruction mimics
the VERB in written human language. Other aspects of the written human language
mimicked in “Computer programming language” are discussed in Section 1.9. How-
ever, there are fundamental distinctions between the “Written human language” and
“Computer programming language”.
However, you should NOT confuse the written human language for computer pro-
gramming language. For example, only prescribed strings of symbols can be used to
label the identity ascribed to the state (NOUN) and transition (VERB) in computer in-
struction. In written human language, a VERB is word ascribed to the motion (action)
of an instance or agency. The metaphor of VERB in computer programming language
is the operator (e.g. Addition, Subtraction, Multiplication) used to manipulate data.
The manipulation of data results in the transition of computer states. Whereas Verb and
Noun are part-of-speech in the grammar of written language, Operator and Operand
are strings of symbols in the syntax of a computer programming language.

6
For example, give the following formulation:

Item 1 HUMAN EXPRESSION = ACTOR + ACTION


Item 2 WRITTEN HUMAN LANGUAGE SENTENCE = NOUN + VERB
Item 3 COMPUTER LANGUAGE INSTRUCTION = OPERAND + OPERATOR

Item 1 above is accessible through human sensory experience alone. An Action and
its Actor are inextricably intertwined and inseparable. Special training is not
required for an habitual users of a language to elicit the message in its expression.
Item 2 is located in a prescribed instrument of language. In that prescription, an Action is
identified as a Verb and its Actor is identified as a Noun. A Noun and its Verb are
separated and individually expressed in a sentence. Special training is required
to elicit the message in a written human language expression.
Item 3 is comprises strings of symbols ascribed to the Manipulation (Operation) of Operand
(Data) in a computer register or memory. The instruction in the message is target-
ted at a material agency (machine) NOT humans.

In this course, therefore, you should NEVER confuse Computer Program-


ming Language (E.g. Python, Java, Fortran), no matter its sophistication, for
Human Written Language (E.g. Yorùbá, Hausa, Igbo, English).
Two or more humans can communicate with each other to the extent of their famil-
iarity with an habitual human instrument of language. Familiarity with human language
is acquired through training and interaction with the habitual users of the language.
Familiarity with programming language is acquired through a careful study of:

(i.) Its Alphabet (Admissible set of symbol)


(ii.) Construction of valid terms using (i.)
(iii.) Construction of valid expression using (ii.)
(iv.) Construction and combination of sequence of instruction using (ii.) and (iii)
(v.) Prescribed standard, convention and process for using (iv) into to write the in-
structions for a program
(vi.) Constant practises with computer programming problem-solving activities.

The more programs you write in a Programming Language, the more your famil-
iarity and competency with its use in computing problem-solving activities.

7
1.2.4 The tools of language
There are there (3) tools in human language. These are:

(a.) Ìhun (Structure): It is used to to hold the terms that are ascribe and/or assign to
the identities of instances of state and transition. Structure is the medium through
which terms (terminal) in an instruction is expressed. A structure and the terms
are used to bring intangible (mental) instance within the ambit of human sensory
organs (E.g. eyes, finger, hear, etc). Indeed, the terms of an expression are in the
Structure of its instrument of language.
(b.) Ìtó.ka (Polarity): It is used to locate and/or arrange the terms into an expression.
The individual terms in an expression can be pointed at by virtue of the language
tool of Polarity. Indeed, the location occupied by each term is by virtue of the
polarity ascribed to it in the structure of its expression.
(c.) Àròtó. (Logic): It is used to formulate the recursion ascribed to a temporal (mental)
instance of state. Logic can neither be seen nor pointed at in the structure of
an expression. Indeed, logic is everywhere present and nowhere located in an
expression.

Àròtó Logic

Èdè Ìtóka Language Polarity

Ìhun Structure

Figure 1.5: Tools of human language

Polarity and Logic manifests through the terms in a structure. In the absences of
structure, polarity and logic cannot be expressed. Whereas polarity is used to arrange
terms into a structure, logic is used to ascribe mental state to the expression in the
structure. Mental state is situated in its human agency.

1.2.5 Examples of Polarity and Logic expressed through structure


If Y = 3 and X = 4

In Polarity operation

(i.) Y + Y = 33
(ii.) X + X = 44

8
(iii.) Y + X = 34
(iv.) X + Y = 43
(v.) Y 2 − 4 =?

Note that NO New Symbols is introduced in the Polarity operation. Every term in
a polarity expression can be pointed at in its structure. Operations are realised through
“Concatenation” (Àsı́npò.) or “Extraction” (Ìfàyo.) only. Note that NO New Symbol is
produce outside of the ones in the input in Polarity operation. Only the symbols in the
input appear in the output. The polarity of every terms is within the ambit of sensory
experience. Logic is not required for processing polarity expression.

In Logical operation

If Y = 3 and X = 4

(i.) Y + Y = 6
(ii.) X + X = 8
(iii.) Y + X = 7
(iv.) X + Y = 7
(v.) Y 2 − 4 = 5

Note that the symbols that are NOT in the input appear in the output of logical
operation. Therefore, the logical operation is NOT in the terms. The operation is in the
logic formulated into the terms. The logical content of a term cannot be pointed at in
its structure. The logical contents of terms are intangible instances in human mental
state.

1.3 What is “A computer”?


Computer is a device with the capacity to accept data and instructions,
process the data using the instruction and produce output in the format
specified in the instruction.

Based on the above definition, the computer can be viewed as depicted in Figure
1.6.
A computing machine is, therefore, expected to have component for:

1. Accepting data and instruction.


2. Processing data following the instruction.

9
DATA
PROCESS OUTPUT
INSTRUCTION

Figure 1.6: Definition of Computer

3. Produce the output of the processed data according to the instruction.

A material agency (machine) or tool that has the capacity to carry out the tasks
listed above is a computer. Indeed modern computers have the capacity listed above,
but they are unlike other machines. What sets the modern computer apart from all the
machines before it is Language. Humans have created a language with which to give
instruction to modern computers. The language is created by imitating written human
languages. The language is called Computer Programming Language.

1.4 History of Modern Computers


The history computer evolves with technology. In the Manual era, material objects
are physically manipulated by humans to assist or support ı̀s.irò (a computing) process.
Fingers, Stones, pebbles, animal bones, pieces of wood and bamboo, etc. are used to
support memory and organisation during a computing process. Specialised tools such
as the Abacus, Slide-rule, O . pó.n-Ayò (Ayo game board See Figure 1.7), Ìlè.kè. Oǹkà
(Counting beads), Okùn Àdı̀kà (Counting ropes) and Apásá (Loom for cloth weaving)
were developed and used during the manual era. The character of the manual era is
that, humans physically and directly manipulate the materials used in the computing
process. The main problem with the manual era is that its computation process is labour
intensive and therefore, prone to error on the grounds of human mistakes. nonetheless,
creativity is most effectively expressed through manual process.

Figure 1.7: Ayo an example manual computing tool used in game playing

10
Table 1.1: Evolution of Computer technology
Ser. Technology Era Strength Limitation
No.
1. Manual tool (Stone, Pebbles, Effective for expressing cre- Labour intensive and prone to
Stick) ativity. errors and mistakes
2. Mechanical (Metal, skin and Reduced the amount of man- Frequent faults and machine
wood) ual labour breakdown
3. Vacuum and Cathode-ray Machine able to work au- Frequent fault and very high
Tube tomatically. Fast operation. maintenance cost.
Machine level programming
through valves and switches
is possible.
4. Diode devices Occupy less space. Low level High Maintenance cost
programming through sym-
bols is possible.
5. Transistor Less energy use. Reduced Difficult High level program-
size. ming
6. Integrated circuit Effective high level Program- Low interconnectivity
ming.
7. Micro electronics High interconnectivity and Low versatility and commu-
cheaper nication speed
8. Nano electronic Seamless interaction and ver- Frequent evolution
satile applications

The manual era is followed by the mechanical era. During this era, metals such as
iron, copper and sliver were refined and used to fabricate devices that automatically give
expression to the state and transition in a computing process. This is the beginning of
the Machine era. A machine (È.ro.) is a material agency that is capable of working “by
itself” (that is autonomously). Example of machines in the Mechanical Era include,
(i) Pascal calculator, (ii) Leibniz calculator and (iii) Charles Babbage’s Difference
Engine. The main problem with the Mechanical era is that its computation process is
unreliable as machines broke down frequently. This makes the output of computations
prone to error. The Mechanical era is followed by the Vacuum and Cathode-ray Tube
era. The Vacuum and Cathode-ray Tube technology makes it possible to store and
manipulate the electrical signals used to represent instances in the state of a computing
process. This technology culminated in faster and more accurate material realisation
of the computing process. Example of machines in this era include Harvard Mark I
and IBM SSEC machines. This Vacuum and Cathode-ray Tube era is followed by
the Diode device era. The major problem of the Vacuum and Cathode-ray Tube is
the heat generated during computing process. Computer memory devices are realised
using mercury delay. The tube and mercury technology consumes substantial energy
as well. This problem was reduced by the Diode device era. The Diode technology
makes it possible to create cooler and more accurate computing machine. Example of
machine in the Diode device era include ENIAC and EDVAC.

11
The Diode device era is followed by Transistor Era. During the Transistor
Era, circuits that implement basic logical operation such as AND, OR and NOT were
fabricated as a single ship. This way, the computing machine during this era consumes
less electricity. Magnetic cores and index register where incorporated into computing
machines. Example of machines in this era include: Honeywell 800, UNIVAC, IBM
7000 Series.
Transistor Era is followed by the Integrated Circuit Era. During this era, elec-
trical resistors, capacitors and transistors, are integrated as a silicon ship. The sil-
icon ships are use manufacture computing machine components. The microproces-
sor emerges during this era. The microprocessor is a device capable of automatically
carrying out arithmetic and logical operation. This makes it possible to manufacture
Calculator-on-a-chip devices. Example of machines in this era include Honeywell 6000
and the IBM 360/370 series. The disused chassis of IBM 370 machine used at the Uni-
versity of Ife. Computer Centre is depicted in Figure 1.8.

Figure 1.8: Plate of IBM 370 machine at the University of Ife. Computer Centre

From about 1975 onwards, Large Scale Integrated Circuit have emerged with
more advancement in technology. The number of electronic components and part that
can be integrated into a single ship has increased tremendously. The modern Nano-
technology era makes it possible to put millions of circuit into a material ship that is
about the size of human index-finger nail.
As a summary, you should note the following in respect of computer technology:

1. The hardware of computer machines evolved with technology;

12
2. The performance of computing machines improve with advancement in technol-
ogy;
3. The cost of computing machines reduce with advancement in technology;
4. The size of computing machines reduce with advancement in technology;
5. The reliability of computing machines increase with advancement in technology;
6. The function and versatility of computing machines increase with advancement in
technology;

Figure 1.9: Evolution of Mobile computing and communication devices

Figure 1.9 (Top) depicts the evolution of Laptop computing machines within a 25
year period. It also depicts (Bottom) the evolution of mobile hand-held devices within
a 40 year period. Figure 1.10 is a date-stamp listing of the evolution of Computer
programming languages between the year 1950 and 2010. Despite the evolution of
computer technology, however, the logic of computing process remains unchanged.
This implies that the logic of addition, multiplication, division, searching, sorting,
and so forth, remain unchanged. This is so even when better technique have been
advanced to achieve computing operation. Therefore, a consistent definition of com-
puting machines should NOT be grounded in technology.

13
Figure 1.10: Date stamp history of Computer programming language

1.4.1 TASK 2
You are expected to read more about the history of computer in standard
textbooks and on the internet. You may be required to submit an assign-
ment on this.

1.5 The Computer System


The computer system is composed of Tangible (physical) and Intangible (non-physical)
aspects. The physical aspect are all the thing that you can physically touch, feel and
move. The Physical aspect of a computer are collectively called the Computer Hard-
ware. Examples of computer hardware include: the mouse, monitor or screen, key-
board, input-output devices such as printer and scanner as well as memory devices
such as your Flash Disk.
The Non-physical aspect are the intangible state and activity of the computer. You
cannot see or touch this aspect but you can know that they are in operation through the
activities of the physical aspect. The intangible aspects of the computer are collectively
called Computer Software. Examples of computer software are: Operating System

14
Software such as Microsoft Windows, UNIX, Android; Application Program Soft-
ware such as Microsoft Excel, Power-point as well as the Apps running on your mobile
phones.
If we use human as a metaphor or analogy, the physical body, i.e. head, legs,
hands, eyes, etc. are the hardware. Human mental activity are the software. Though
you cannot see human mental activity, but you could see the physical activities through
his/her action. However, human physical activities are the manifestation of human
mental activity. Hence, human mental and physical aspects work in harmony in hu-
man activities. Similarly, the hardware and software must work in harmony before the
computer can function properly.
The next section introduces you to the hardware components of the computer. The
components discussed here are necessary for understanding how the computer program
works. There are other hardware components of the computer which you do not really
need to know much about before you can write a program. For example, the computer
motherboard and power supply unit.

1.6 Computer hardware configuration


The structure of computer hardware is depicted in Figure 1.11. The structure comprises
parts that are connected and configured to work together during the computer operation.
In the figure, the dotted arrow head line is control signal and the full arrow lines are
the data lines.

Figure 1.11: Structure of a Computer

Hardware are physical devices constituting the parts that facilitates a computer
program execution.

15
1.6.1 Input-Output devices
The input component is also called the input device. Examples include Keyboard,
mouse, touch screen, pen and so on. The ability to use the Keyboard and Mouse is
central to computer programming.

1.6.2 Central Processing Unit


The Processing component comprises a number of other components that facilitate
the execution of a programme. The Computer Process Unit comprises three (3) other
subcomponents:

Memory Element (ME)

This is the place where the computer stores results of ongoing operation. The memory
element are inside the processor. They are also called register. Each of the register
inside a process severs a specified purpose. An example is the Accumulator Regis-
ter, which stores the results of an ongoing arithmetic operation. Another example is
the processor Status Register, which stores the status of an ongoing operation, such
as when “an overflow” or “negative” result is generated. The Memory element can be
likened to a scratch pad that you use while solving mathematics problem: a place where
rough work is written. The content of the Memory Element or register is lost imme-
diately power is switched off from the computer. This is why the Memory Element of
register is called a Volatile Memory.

Control Unit (CU)

The Control Unit determines the operation of all the other hardware component of the
computer. However, the activity of the Control Unit is determined by the Computer
Software. The Control Unit (CU) will, for example, determine when the input device
must read data, when the output device must write output and when data and instruction
are transmitted between devices. The CU reads program instructions from the computer
memory and interprets (decodes) the instructions. It then uses the interpreted instruc-
tion to determine what is to be done. To achieve the tasks specified in the instructions,
the CU generates a series of control signals to the other parts of the computer hardware.
When everything seems to be working well but nothing is working, the control unit is
probably faulty.
Control units in advanced computers may change the order of some instructions
so as to improve performance.

16
Arithmetic and logic unit (ALU)

The ALU carries out arithmetic and logical operation. It arithmetic operations include:
Addition (+) and Multiplication (×). Its logical operations are those that evaluate to
True or False. Examples includes Greater Than (>), Less than (<). Most computer in-
structions are realised through these simple operations. It turns out that by performing
these simple operations, a computer can be programmed to achieved complex tasks.
This is done by breaking the complex tasks into simple steps that are realised through
the operation of the Arithmetic and Logical Unit. Therefore, modern computers can
be programmed to perform a well-defined task; although it will take more time to do
so if its ALU does not directly support the operations in the task. An ALU may also
compare numbers and return boolean (true or false) value depending on whether one is
equal to, greater than or less than the other (“is 64 greater than 65?”). Logic operations
also include boolean logic: AND, OR, XOR and NOT. These can be useful both for cre-
ating complicated conditional statements processing boolean logic. Modern computers
(e.g. Superscalar machines) contain multiple ALUs so that they can process several
instructions at the same time. Graphics processors and computers with SIMD (Single
Instruction Multiple Data) and MIMD (Multiple Instruction Multiple Data) features
often provide ALUs that can perform arithmetic on vectors and matrices.
Memory elements (ME), Arithmetic and Logical Unit (ALU) and Control Unit
(CU) are together called the Central Processing Unit (CPU) of the computer.

CPU = ME + ALU + CU

The computer’s capacity to carry out small to large scale processes, its CPU must
be supported by Memory Devices.

1.6.3 Memory devices


Memory Devices are the hardware components of a computer in which
data and instruction are stored.

There are two categories of memory devices: (i) Main memory and (ii) Mass
memory. These are discussed in the following subsection.

Main memory devices

The Main memory is the memory device in which the computer stores the data and
instruction in the currently running program. The computer store temporary result of
operation in this memory. When a program is executing, its data and instructions are
store in the Main memory. The main memory is a Random Access Memory (RAM).
This implies that the computer can access data in the memory at any location of the
memory without delay and in any order. Main memory is usually smaller in size than

17
the Mass memory. It is also more expensive than the mass memory. A major weakness
of the Main memory is that when electric power is lost, its contents will be lost as well.
This is why it is called Volatile memory. The Main memory is also called Primary
memory or Core memory. The data and instruction that we wish to retrieve latter are
stored in the Mass memory. This is discussed as follows.

Mass memory devices

The Mass memory is the memory device in which the computer stores data and in-
struction that can be retrieved latter. When a program is NOT executing, its data and
instructions are store in the Mass memory. The mass memory is a Sequential Access
Memory (SAM). This implies that the computer can access data in the memory in the
sequence or order they are store in the memory. Mass memory is usually much larger in
size than the Main memory. The Mass memory is also cheaper than the Main memory.
A major strength of the Mass memory is that when electric power is lost, its contents
are retained. This is why it is called Non-Volatile memory. A major weakness of Mass
memory is that it is slower than the Main memory. The Mass memory is also called
Secondary memory or Peripheral memory. There are other memory devices, such
as the Read only Memory (ROM) and Programmable Read only Memory (PROM). In
these memory devices, specialised data and instructions are stored. For example, the
data and instruction that the computer requires for its intrinsic operations are kept in
these specialised memory devices. Examples of such specialise data and instruction
are those that the computer system use during “start-up” and “Booting”. The computer
timing data is also stored in Specialised memory devices. This class of memory are
usually of interest to “Embedded” or “Real-time” system programming.

1.6.4 Memory metric


Computer memory devices are of various sizes. The different sizes of computer mem-
ory are reckoned using a metric based on the number of cells that makes up a unit of
data. The Bit (Binary Digit) is the smallest data element in modern digital computing
machines. The Bit is stored in a Cell. Therefore, a cell holds a Binary digit (Bit) dur-
ing computer operation. Each cell holds either Zero 0 or One 1 at an instance in
the computer operation. Four (4) cells, each of them holding a bit, e.g. 0 1 0 1 ,
makes a Nibble. A unit of data in modern computer is a Byte. A computer Byte
comprises Two (2) Nibbles or Eight (8) Bits, e.g. 0 1 0 1 0 1 0 1 . The number of
unit data comprising the entire storage space in a memory device is reckoned in Bytes
as depicted and described in Table 1.2. A Byte can hold one character, E.g. the letter
‘R’, the digit ‘5’, etc. Memory metric is, therefore, a rough estimate of the number
of characters that can be stored into a memory device. If the number of character in
your notebook is up to Ten thousand (10, 000), then you will need a memory of about
Ten Kilobyte (10KB) to store it. This is estimated as 10 × 210 Bytes, where 210 Bytes

18
corresponds to about One thousand (1,000) characters.

Table 1.2: Computer memory metric


Ser. Name Description
No.
1. Bit A status of the computer cell. It can be Zero (0) or One(1); 0 and 1
2. Nibble Four bits. 0 1 0 1
3. Byte Eight(8) Bits or Two (2) Nibble 0 1 0 1 0 1 0 1
4. Word The number of bytes that a machine can process at one instance of its
operation. It is the number of lines in the data bus of the computer.
5. Kilobyte (KB) A Kilobyte is computed as 210 (1024) Bytes.
6. Megabyte (MB) One Megabyte is computed as 220 (1,048,576) Bytes.
7. . Gigabyte (GB) A Gigabyte is a computed as 230 (1,073,741,824) Bytes.
8. Terabyte (TB) A Terabyte is computed as 240 (1,099,511,627,776) Bytes.
9. Petabyte (PB) A Petabyte is computer as 250 (1,125,899,906,842,624) Bytes.
10. Exabyte (EB) An Exabyte is computed 260 (1,152,921,504,606,846,976) Bytes.

19
1.7 Computer Software
The intangible aspect of the computer system are called “Computer Software”. Com-
puter Software often comprises several “Computer Programs”. If a program, or set of
programs, fails, the computer hardware may not function properly. The human who
writes program for controlling a computer is called a Computer Programmer. The
humans or group of humans involved in the Specification, Design, Programming (Cod-
ing) and Evaluation (Testing) of software are also called Software Developer. Every
programming for problem-solving involves familiarity with every aspects of software
development. The job of a computer programmer (software developer) is to design
and implement a set of programs that will make the computer to do what a user
wants. Usually the user is a human being. The user can also be another computer.
For the programmer to achieve this task he/she must first understand the problem that
the user wants to use the computer to solve. The programmer must also understand
the capability of the computer as well as how to instruct the computer to perform the
specified task. In this course you will learn how to instruct a computer using the
Python programming language.

A software comprises a set of programs that work together during the


execution of a process.

Computer software are of Two (2) categories:

(i) System software.


(ii) Application software

These are explained in the following Subsection.

1.7.1 System software


System software is specifically developed by computer manufactures to make the com-
puter ready for use by other programs and users.

System software is a set of programs that are used to create the environment
for other software to run efficiently on the hardware of a computing machine.

System software performs the following tasks:

1. Create the environment for other programs to run. [Run is the terminology used
for “a process executing on a computing machine”]
2. Manage the computer resources (disks, memory, printers, etc. ) for effective use.
E,g, Data Zipping (Compress data into smaller memory size)

20
3. Provide data about the status of the hardware and related computer component
[E.g. time].
4. Provide prompts (message) that can help the user in computer usage.
5. Provide interfaces that suit users fancy and appeal;
6. Respire or reclaim unused computer resources such as memory. System Software
that reclaims memory are call “Garbage Collector”.
A very important System program is the Operating system. Examples of operat-
ing systems include:
1. UNIX
2. LINUX
3. Ubuntu
4. Solaris
5. Mac OS
6. Windows XP
7. Android OS
Other examples of system programs includes: Editor, Linker, Loader. System
programs are usually written by System programmers. System programs are commis-
sioned by specialised Computer vendors, organisation or Business interests. Aspects
of system programs are usually written using Low Level Language called Assembly
Language.

1.7.2 Application software


An Application Package (Apps) is a collection or suit of software intended for solving
problems in a particular user environment. Each of the set of programs in an Applica-
tion software is for achieving specific user task. For example, a set of program can be
written for assisting with all the work done in the office. The work may include doc-
ument typing and processing also called word processing. Keeping a “table of inven-
tory” also called spreadsheet. Other program may be written for “drawing of graphics”
and/or “Architectural Design”. Some others may also be written for music compilation
and processing. An example of Application Package is Microsoft Office. This pack-
age comprises (i) MS Word (for word processing) (ii) Excel (For spreadsheet) (iii)
MS FrontPage (For graphic design) (iv) MS Publisher (For publishing works), (v) Ms
PowerPoint (For making presentation slides), etc. Another examples is OpenOffice. It
comprises similar applications like Microsoft Office. Other examples of Application
packages include:

21
1. In Mathematics, Science and Engineering [Octave, Matlab, Mathematical and
Maple]
2. In Internet browsing [Google Chrome, Safari, Firefox]
3. In Graphics design and Architecture [AutoCard, Adobe Photoshop, CorelDraw]
4. Online meeting [Skype, Hangouts, Google Meet, Zoom, and Whatsapp]
5. Banking [Oracle, TEMENOS, CorePlus, Bankware]
6. There are many Application programs (Apps) on modern mobile devices.

As you are already aware, there are Application programs (Apps) on your Mo-
bile Phone for doing all sort of tasks from banking, to playing games, searching for
location, and so forth. These Apps cannot run except there is a System program e.g
Android, already running on the hardware of your Computer or Phone. Application
programs are written by Application programmers. Substantial parts of Application
programmers are written using High Level Programming languages such as Python,
Java, C++, Pascal, FORTRAN.

1.8 What is Computer Programming?


Computers will do what humans instruct. The instructions for computers are written in
a Programming Language. Computer will NOT do what they are NOT programmed to
do. To ask a computer to perform a task, you have to give it Two (2) things:

1. The data on which the computer will perform the task.


2. The set of instruction on how to manipulate the data.

After applying this instruction to the data the computer produces an output. This
means that all what you want the computer to do must be reduced into data and in-
structions. While following the instruction, the computer will be manipulating the
data. If we adequately describe our instruction and accurately specify our data, then
the output that the computer generates will be what we want.
The art of composing the sequence of instructions for controlling computer
system operation is called programming.
The outcome of a computer programming activity is a program.
A program is a sequence of instructions which when executed by a computer,
the output corresponding to an input data will be produced.
An instruction is a unit of message or command that influences the operation of a
computer. An instruction is composed of Two (2) parts:

22
1. Operand (Data)
2. Operation (Operator)
INSTRUCTION = OPERAND + OPERATION
INSTRUCTION = DATA + OPERATOR
An analysis of the instruction:
ADD Two (2) data items identified with the names X and Y and Store the output in Z
is as depicted in Figure 1.12.

Figure 1.12: Fundamentals of Data and Instruction

1.8.1 Operand: Data


Names are used to identity the entity or item or element that the computer will manip-
ulate during a process. Individual item or entity is a datum. Instances of datum are
called Data (The plural of DATUM is DATA).
A name or identified that can contain only one value when a program is running
is called Constant name. An example of Constant name definition is P i = 3.147. A
name or identifier that can contain different datum when a program is running is called
Variable name. An example of Variable name definition is Radius = 7.5. A con-
stant or variable name can be occupied by different datum or data include: Numerical
(Number), Literal (Text) or Logical (True/False).
Several data item can also be identified with One name. This is done in the defini-
tion of: (i) List , (ii) Array, (iii) Dictionary and (iv) Tuple

1.8.2 Operation: Operator


Operators prescribes the manipulation that will be performed on operands (Data, datum
variable). This include Arithmetic operation on constants such as Addition and Mul-
tiplication; Algebraic operation on variables such as Subtraction and Division. It also

23
includes; Logical operation Negation and Inversion; Input-Output operation such as
input and print.

1.9 Foundation of Computer Programming Language


The process of programming can be viewed as the process involved when two (2) hu-
man individuals communicate through Writing Letter. The two (2) individual are
communicating using the written form of their habitual instrument of language. Their
habitual instrument of language is Human Language. In this case, however, the Pro-
grammer is communicating with a machine using a computer Programming Lan-
guage. Indeed the Program writing process is grounded in the metaphor of Letter
writing.
These are the attributes of the Letter Writing process
1. The message in a letter is written in a Human Language { e.g. Yorùbá, Igbo,
Hausa, English, etc. }.
2. The symbols in the message are drawn from the alphabet of the Human Language.
3. There is a format of presentation to which the text (narrative) in the letter conforms
{e.g. formal and informal letter }.
4. There is an opening (beginning) and a closing (ending) statement.
5. There are sequence of words, phrases, sentences and paragraphs between the
Opening and Closing statement. Each of the words, phrases, sentences and para-
graphs expresses aspects of the message in the letter. Each word, phrase, sentence
and/or paragraph is composed using the symbols drawn from the alphabet.
6. The message in the letter conveys information from the Writer (Sender) to the
Reader(Receiver).
Note that a letter is written by humans and the instruction or message in it will be
read by another human being.
In the same manner a computer program is written by human being (a Program-
mer) but the primary target or audience of the instructions or messages a machine (the
computer). A program:
1. A computer instruction is written in a Programming Language { e.g. Python,
FORTAN, JAVA, C++, VisualBasic, etc. }.
2. The symbols for composing an instruction are drawn from the alphabet of the
computer language.
3. There is format to which admissible instruction must conform {e.g. this is called
the Syntax or Format of instruction }.

24
4. A program has an opening and closing statement
5. There are sequence of instruction, functions and sub-programs between the Open-
ing and Closing statements.
6. The program conveys the writer (programmer’s) instructions to the computer (re-
ceiver).
The following subsections, and indeed, other aspects of this course will explicate
the above analogy.
To compose an effective computer program, it is important to be familiar with the
Programming Language and Programming Process. Familiarly with Programming
Language includes:

1.9.1 Familiarly with Programming Language


Familiarly with a computer Programming Language involves:
1. Identifying the assignment and use of symbols in alphabet of the programming
language. This include special or reserved symbols and strings.
2. Understand how to compose the string of symbols for naming data or operand.
3. Understand how to use data and operators to compose valid instructions.
4. Understand the admissible use of instruction individually and collectively.

1.9.2 Python Instruction format and structure


During this course, the format and structure of the instruction for following Python
statements and expressions will be explained with examples. You will also be intro-
duced to other formats and structures that are used in the Python Object Oriented Pro-
gramming Language. In addition, you will learn the techniques for using the format
and instruction in programming. Some of the instruction and statement formats and
structures that would be consider include:

1. Data formatting.
2. Input and output statements.
3. Decision, Control and Conditional Statements.
4. Loops and Looping.
5. Array data structures.
6. Routings and Functions: Modular Programming (Breaking a complex program-
ming problem into Function of smaller problems).

25
7. File processing.
8. Object Oriented Programming (Definition and use of Class of Object)

NOTE THAT

(a.) Python is CASE sensitive. Therefore Ade and ade are NOT Equal.
(b.) Python is space sensitive. Space must be used as prescribed by the syntax of an
instruction for example in the: (i) if (ii) for and (iii) while instruction.

1.9.3 Python Identifiers


The variables in a Python program can be likened to the nominal part-of-speech in
human written language. The nominal part-of-speech is a NOUN. The “Noun” is a
class of strings, each of which is used to identify an instance or agency. Variable can
hold one constant at an instance during a computer operation. Therefore, the content
of a variable can change during the execution of a process. The constants in a Python
program can be liken to the words in human written language. The “word” is a string of
symbols used to identify a datum, data, or agency (i.e. Function or Object). Constants
do NOT change during the execution of a computer program. An example of a constant
identification is P i = 22/7 or P i = 3.147. When the value to be stored changes then
it is given a variable name, for example Sum or Average. Variable names correspond to
the identity of a memory location in a computer system. In the case of a constant, the
exact value of the constant, e.g. “Ade”, “1234”, and “Lagos” is stated. In the expression
NAME = “Ade” , the string NAME is a variable while the string “Ade” is “an instance
of constant” that can occupy its content. Also in the expression Y = 10 , the identifier
Y is a variable while the number 10 is a constant. The number 10 can also be called a
constant string of Two (2) digits, that is 1 and 0.

1.9.4 Python Reserved words


There are certain names that have special function in the Python programming lan-
guage. These names are called Reserved words or Key words. Reserved words
CANNOT be used to name a variable or constant. Thirty-Tree (33) Python Reserved
words are listed in Table 1.3. Selected Python operator are listed in Table 1.4 and details
of their uses and precedence is provided in the Appendix A in Section 1.15.

1.9.5 Python Variable and Constant identifiers


Name is used to ascribe identity to an instance. The name of a constant or variable is
composed through that concatenation of letters and digits. The composition is similar
to how words are composed from written language alphabet. A string corresponding to
a valid Python name is constructed using the following set of rules:

26
Table 1.3: Python Reserved or Key words
true elif try return def in raise
and else while lambda del is from
as except with nonlocal break import finally
assert finally yield None continue if
class global pass not false for

Table 1.4: Python Operators


+ * – % ** // <<
Add Multiply Subtract Remainder Power Floor Di- Shift left
vision
>> & | <= > >= <>
Shift Bitwise Bitwise Less or Greater Greater Not
right ADD OR Equal or equal Equal
!= / ==
Logical Divide Equal
not Equal logical

1. A variable or constant name can only be composed using the Python set of sym-
bols. This includes upper case letters {A, B, C, ..Z}, lower case letters {a, b, c,
..z} and digits {0,1,2,3,4,5,6,7,8,9}.
2. Variable or constant names are case-sensitive. This implies that Number, number
and NUMBER are three (3) different names.
3. A variable or constant name CANNOT be any of the Python keywords.
4. A name must contain not more than Thirty-one (31) characters. This implies that
the length of a name should not be more than 31 characters.
5. A number should not start a name. For example, the variable 5name is wrong but
Name5 is correct.
6. A special character (except the underscore, ), should not be used in forming
a name. For example, the variable Fn*ame is wrong but the name Fn ame is
correct.

1.9.6 Python Statements


A Python statement is an instruction, which when executed, will influence the com-
puter operation. A statement is similar to a sentence in human languages. Each of the
statement in a program is also called a code. Various Python statement and how they
are used are discussed in your Laboratory and Tutorial Manual. Nonetheless, note that
Python program state is SPACING SENSITIVE. This implies that SPACE can influence
how a sequence of instructions are Executed.

27
1.9.7 Familiarly with Programming Process
Familiarly with Programming Process includes:
(i) How to design the algorithm of a programming process
(ii) How to reduce the algorithm into program code or instruction.
(iii) How to create the digital version (the computer text) for the instruction composed
in (ii)
(iv) How to compile the digital version in (iii) into computer native language.
(v) How to identify and remove errors and mistakes from your code.
(vi) How to document your code for future use and improvement.
Familiarly with Programming Process is briefly discussed in the following sec-
tions.

1.10 Algorithm
The origin of the term Algorithm was traced to a 9th century Arabic scholar called Abu
Ja’far Muhammed Ibn Musa Al-Khowarizm. An algorithm is a sequence of formal
instruction describing the solution to a computing problem. An algorithm describes
the solution one problem only. This implies that when an algorithm is written for a
problem it cannot be used to solve a different problem.
The following are the features of an algorithm:

1. [Terminal:] There is exactly one ENTRY (START) and one EXIT (END) points
in the algorithm. These are called the terminals of the algorithm.
2. [Finite process:] The steps in the algorithm must be finite. The number of steps
from when the input is applied and an output is generated must be totally count-
able. A step corresponds to an instruction.
3. [Effective:] The algorithm must produce the total output corresponding to the
input data.
4. [Soundness:] The algorithm must produce the same output for an input no matter
the number of times the input is applied.
5. [Practical:] An algorithm must be executable on a computer system.
6. [Halt:] It is desirable that the program implemented using an algorithm releases
all computer resources (memory, register, data bus, processor) immediately after
its termination.

28
The instruction in an algorithm and how they are structured follows a standard set
of rules. An algorithm intended for a program is design before it is reduced into codes
(set of instruction) and implemented on a computer. Algorithm designs are primarily
intended for use among programmers during the programming process. An algorithm
is, therefore, an important aspect of computer programming just as the Plan is important
in Building a house.
There are two (2) popular tools for designing an algorithm. These are: (i) Pseudo-
code and (ii) Flowchart. A pseudo-code uses instructions written out in long-hand as
well as mathematical statements to express an algorithm. This is done in a fashion
similar to human written language discussed above. Pseudo-code are usually not very
explicit as their interpretation often require an understanding of the rule for writing the
pseudo-code expressions.
In the flowchart approach to algorithm design, however, Geometric images are
used to represent instructions. Familiarity with only a few geometric images or symbols
is required for flowchart algorithm design. We will be using the flowchart more often
in this course. Some important flowchart symbols are shown in Figure 1.13. We will
discuss them further during our lecture.

1.11 Computer program development process


To develop a program for the solution a problem using the Python programming Lan-
guage, you need to follow some steps.

Step 1: Understand the problem: It is good to always start your programming by


giving the problem it solves a NAME. The first step in computer problem solving
is to understand the problem. To do this, you need to take a simple case exam-
ple and solve the problem manually. This will allow you to better understand the
mental activity involved in problem-solving process. The outcome of this process
is a PROGRAM SPECIFICATION DOCUMENT containing a description of
the solution process.
Step 2: Identify Input and Output: It this step the Input data are listed with their
data types and sample values. Also, the expected outputs are listed and described.
The goal of this step is to assign identifiers to each variable and constant in the
Input and Output of the problem-solving process. The outcome of this step is a
DATA DICTIONARY of the program.
Step 3: Formulate the process: The individual manipulation operations required for
transforming the input data to its corresponding output data are listed. This will
include: what calculations are required and how they will be invoked. You may
need to consider a number of alternative solution methods at this stage before
settling for the most appropriate for the problem at hand. The outcome of this

29
Figure 1.13: Flowchart Symbols

process is a PROCESS DICTIONARY. This comprises a list of the methods,


functions and routines that will be execute on the input to produce the final output.
Step 4: Solution Algorithm Design: Formulate an algorithm to realise the Steps 2
and 3. This is the program design. To achieve this, you need to use design tool
such as Flowchart symbols and/or Pseudo-code to construct the sequence of steps
in the problem-solving into a unified process. It may be the case that the solution
will be implemented by breaking a bigger problem into smaller ones. Each of the
smaller problems is then implemented as a module. The outcome of this step is
the SOLUTION DESIGN. The design is the basis for all other future activity that

30
concerns problem-solution process.
Step 5:Implement the Design: In this stage of the problem-solving, you need to con-
vert the algorithm designed in Step 4 into a computer program. Here you will
reduced the Flowchart or Pseudo-code into Python instructions. The tasks here
includes:

(i) Creating the source file. Here you type the program (from paper) into a com-
puter Text Editor.
(ii) Compiling the program. The program text will be applied to Python Compiler
software which will check it for Syntax error. If there are syntax errors (or
warning in your program) you will need to correct it in your source file. If no
error is found, the system will generate the object or executable file.
(iii) Repeat steps (i) and (ii) until your program is running as desired.

The outcome of step is a RUNNING PROGRAM.


Step 6: Program Testing and Documentation Run the program with the Input data
you used in Step 1. This is to confirm that the output you computer manually is
also what the computer generates. Thereafter, select a number of example input
data that you already familiar with their outputs. If the output produce by your
program is not correct, you need to go back to the design [Step 4] of the solution.
If the incorrect result persists, then you move to Step 3, and so forth. The outcome
of this step is a PROGRAM TESTING AND DOCUMENTATION. The document
produced in this step will contains a description of how you carried out steps 1 to
Step 5 as well as how the outcome of the testing of your program. The document
can also contain your views and comments on the extent of the application of the
program.

1.11.1 TASK 3
Type and run the following three (3) codes into the Python environment that you in-
stalled on your mobile phone. Explain your observations after running the programs.
(HINT: See Subsection 1.2.5).

y = input(“Enter a digit”)
x = input(“Enter another digit”)
CODE 1
print (y+x)
print (x+y)

31
y = input(“Enter a digit”)
x = input(“Enter another digit”)
y = int(y)
CODE 2
x = int(x)
print (y+x)
print (x+y)

n = input(“Please enter a number”)


y = n+n
CODE 3 x = int(n) + int(n)
print(“The polarity sum is ”, y)
print(“The logical sum is ”, x)

1.12 Case example: FindAverage


We will demonstrate the software development process discussed in Section 1.11 by
developing a program to find the average of Five (5) numbers.

Understand the problem: The name of the program is FindAverage. The task of
finding the average of Five (5) numbers involves: (i) Finding the Sum by adding
the numbers together and (ii) Dividing the Sum by 5. We will assume that the
numbers are of type Real. Type Real are numbers with factional part. For example,
5 is an Integer because it has no fractional part, but 5.0 is a Real number because
it has a fractional part; even when the fractional part is zero.
The command f loat is used to convert an input into a real number in Python.
Therefore, an input data is converted to real number by applying the f loat com-
mand to it.
The command int is used to convert an input into a integer number in Python.
Therefore, an input data is converted to integer number by applying the int com-
mand to it.
If we assume that the numbers we wish to find the Average are:
• 5.0
• 8.0
• 7.0
• 9.0
• 24.0
We will
1. Sum the numbers, i.e. 5.0 + 8.0 + 7.0 + 9.0 + 24.0 = 53.0

32
53.0
2. Divide the Sum by 5, i.e. 5.0 = 10.6
Identify the input and output: The input to the problem are the five numbers: let us
label them using the variables:
N umber1, N umber2, N umber3, N umber4, N umber5
The output from the program is the average: let us label it with the variable name:
Average.
Formulate the process: The process require for computing the output from the input
are as follows:
Sum = N umber1 + N umber2 + N umber3 + N umber4 + N umber5
and
Sum
Average =
5.0
Design the solution algorithm: The solution algorithm is represented by the flowchart
in Figure 1.14.

Start

Read Number1
Read Number2
Read Number3
Read Number4
Read Number5

Sum = Number1 + Number2 +


Number3 + Number4 + Number5

Average = Sum/5.0

Write Average

Stop

Figure 1.14: Design for the Average Program

Implement the solution: To implement the design, create the source file by typing the
following program code into the Python editor.
Run the program as instructed in the CSC201 Laboratory and tutorial manual.

33
Table 1.5: Python code for the Flowchart in Figure 1.14

Number1 = float(input("Enter Number1: "))


Number2 = float(input("Enter Number2: "))
Number3 = float(input("Enter Number3: "))
Number4 = float(input("Enter Number4: "))
Number5 = float(input("Enter Number5: "))
Sum = Number1 + Number2 + Number3 + Number4 + Number5
Average = Sum/5
print("Average of the numbers is :", Avegrage)

Test the Program: Run the program using the data used in Step 1 and see your output.
Now run the program again with at least Three (3) other set of input of your choice.

Start

Sum = 0.0
NextNumber = 0.0
Average =0.0
Item =0

Read NextNumber

Sum = Sum+ NextNumber


Item = Item +1

No
Item = 5?

Yes

Average = Sum/5.0

Write Average

Stop

Figure 1.15: Design for the Average Program

The program in Table 1.5 will work only when the number of input is Five (5).
What this implies is that even if you want to find the Average of Three (3) numbers,

34
Table 1.6: Python code for the Flowchart in Figure 1.15

item = 5
Sum = 0.0
for n in range(item):
NextNumber = float(input("Enter the next number : "))
Sum = Sum + NextNumber
Average = Sum / item
print("Average of ", item, " numbers is :", Average)

the program will not work. Also, the instruction to read every number is in the code.
Most often, programs are designed to be more flexible. The flowchart in Figure 1.15 is
the design for a program that can read a specified number of input data and output their
average. The Python code for the flowchart design in Figure 1.15 is in Table 1.6.
The Python program codes in Table 1.5 and Table 1.6 will work only when the
number of input is Five (5). What this implies is that even if you want to find the
Average of Three (3) numbers, the program will NOT work. Most often, programs
are designed to be more flexible. To improve the flexibility of the program, it will be
designed to be able to handle various number of input data.

1.13 TASK 4
The Python code to find the average for any number of input data is in Table 1.7.
You are to carry our the Program development Steps above in respect of the code.

Table 1.7: Python code for flexible Average computation

item = 0
Sum = 0.0
item = input("Enter number of items to average: ")
item = int(items)
while n < item :
NextNumber = float(input("Enter next number : "))
Sum = Sum + NextNumber
n = n + 1
Average = Sum / item
print("Average of ", item, " numbers is :", Average)

35
Table 1.8: Another Python code for flexible Average computation

item = 0
Sum = 0.0
item = input("Enter number of items to average: ")
item = int(items)
for n in range(item):
NextNumber = float(input("Enter next number : "))
Sum = Sum + NextNumber
Average = Sum / item
print("Average of ", item, " numbers is :", Average)

1.14 TASK 5
Using the Python environment installed on ON YOUR MOBILE PHONE, carry out
the following tasks:

1. Enter and run the following code. Explain your observation about the code.

i = input (“ Enter the number of times for looping”)


i = int(i)
for k in range (0,i)
print (“ Loop”, k)

2. Enter and run the following code and explain what it is doing. Replace “Odetunji”
with your own name, rerun the program and explain its output. Replace the line
y = “Odetunji” with y = input (“What is your name”) rerun the program and
explain its output.

k=0
y = “Odetunji”
for i in range(len(y))
x = y[i]
k = k +1
print(x)
print (“The number of letters in your name is ”, k)

3. Modify the code in 2. above such that it can read any name and print the number
of letters in it.
4. Enter and run the following code on your phone. Draw a flowchart to explain what
the code is doing.

36
Num1 = input (“ Please enter first number ”)
Num2 = input (“ Please enter second number ”)
Num1 = int(Num1)
Num2 = int(Num2)
if Num1 > Num1:
print (“First number is greater than the second”)
elif Num2 > Num1:
print (“Second number is greater than the first”)
else:
print (“The numbers you entered are equal”)

5. Modify the code in 3. above such that it can read any name and print the number
of vowel letters in it. For example your program should output 5 for the name
“Odetunji”. Hint: You need to use the Python if statement here.
6. Write an encryption program which reads a string comprising lower-case letter
in the English language alphabet. Your program will then encode the string by
replacing each letter with its corresponding alphabetic position. The following
replacement will be used in the encryption a = 1, b = 2, c = 3, d = 4, e =
5, ...z = 26. For example your program should output 145 for the string “ade”.
Hint: You need to use the Python if statement here. You may also consider
Python list data structure, that is Letters = [a, b, c d, ..]

37
1.15 Appendix A

Figure 1.16: Python Programming language operators

Figure 1.17: Precedence of operator in Python programming language

38

You might also like