Lecture 2 - Developing Programming Skills
Lecture 2 - Developing Programming Skills
Aspects of
programming Mathematical
PYTHON basics
languages, language operations.
levels, writing code.
PYTHON variables
and types.
Sunday, January 13, 2019 NDU - ENG 202 - © Maurice J. KHABBAZ, Ph.D. 2
Developing Programming Skills
• Learning to program a computer:
• Training to solve problems in a very detailed and organized manner.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 3
Computer System Organization Preliminaries
• Computer system is composed of:
• Hardware:
• Physically tangible pieces (e.g. chips, wires, electronic boards, etc.)
• Useless without instructions to tell them what to do.
• Driven by a series of instructions (a.k.a. programs).
• Software:
• Programs and the data those programs use.
• Non-tangible counterpart of hardware.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 4
Basic Computer System Hardware Architecture
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 5
Basic Computer System Hardware Architecture
MEMORY
INPUT OUTPUT
DEVICES DEVICES
CPU
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 6
What Does A Computer System Do?
• Essentially, a computer system:
• Performs calculations (i.e. billions of calculations per second).
• Remembers results (i.e. hundreds of Gigabytes of storage).
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 8
Types Of Knowledge – Cont’d.
• Numerical Example:
• The square root of a number ! is y such that " × " = !.
• Recipe for deducing square root of a number ! = 16:
1) Start with a guess, '.
2) If ' × ' is close enough to !, stop and say ' is the answer.
( Control of flow!!
3) Otherwise make a new guess by averaging ' and ).
4) Using the new guess, repeat the process until the answer is close enough.
* *×* + +
* +*
*
-
3 9 16/3 = 5.3333 4.17
4.17 17.36 3.837 4.0035
4.0035 16.0277 3.997 4.000002
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 9
What is a Recipe?
• A recipe is:
1. It is a sequence of simple steps.
2. Flow of control process that specifies when each step is executed.
3. A mean to determine when to stop.
• Definition: 1. + 2. + 3. = an algorithm.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 10
Writing A Recipe and Implementing A Program
Implementation Phase
No
Verification
No Code
OK?
OK?
Yes Yes
No Yes
OK?
Maintenance
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 11
Programming Shortcut
Problem
Solving
Problem Phase
(Algorithm)
Shortcut?
Implementation
Phase
(Writing Code)
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 12
Algorithm Implementation
Georgio’s
Different Programming Languages
Code
Algorithm
Different Programmers
C#
C++
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 13
Capturing A Recipe In A Mechanical Process?
• To capture a recipe in a mechanical process:
• Stored program:
• Set of instructions.
• Instructions built from pre-defined set of primitives:
• Arithmetic and logic.
• Simple tests.
• Moving data.
• Instructions are then executed by the machine.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 14
Basic Primitives
• Alan Turing developed the Turing Machine:
• A mathematical model of computation that defines an abstract machine:
• Manipulates symbols on a strip of tape according to a table of rules.
• A Turing machine capable of simulating any algorithm’s logic can be constructed.
• An instruction set/programming language is Turing Complete if:
• It can be used to simulate any Turing machine.
• Turing showed that anything can be computed using six primitives.
• Modern programming languages:
• Have more convenient set of primitives.
• Can abstract methods to create new primitives.
• Anything computable in one language is computable in any other language.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 15
Creating Recipes
• A programming language provides a set of primitive operations.
• Expressions are:
• Complex.
• Legal combinations of primitives in a programming language.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 16
Aspects of Languages
• Primitive constructs:
• English language: words.
• Programming language: numbers, strings, simple operators.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 17
Aspect of Languages – Cont’d.
• Syntax:
• English language:
• “cat dog human” → not syntactically valid.
• “cat eats human” → syntactivally valid.
• Programming language:
• “hi” * 5 → not syntactically valid.
• 3.2 * 5 → syntactically valid.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 18
Aspects of Languages – Cont’d.
• Static semantics are which syntactically valid strings have meaning.
• English language:
• “I are hungry” → syntactically valid but static semantic error.
• “I am hungry” → syntactically valid and static semantic valid.
• Programming language:
• 3.2 * 5 → syntactically valid.
• 3.2 + “hi” → static semantic error.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 19
Where Things Go Wrong
• Syntactic (a.k.a. syntax) errors:
• Common and easily caught.
• Static semantic errors:
• Some languages check for these before running a program.
• Can cause unpredictable behavior.
• No semantic errors but different than what programmer intended:
• Program may:
• Crash.
• Stop running or runs indefinitely.
• Gives an answer different than the one expected.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 20
Programming Language Levels
• In a computer data is represented by binary electric pulses:
• ON – TRUE – YES →‘1’
• OFF – FALSE – NO →‘0’
• Machine Language:
Low-Level languages
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 22
Program Development
• Software tools involved:
• Editor: used to type a program and store it in a file.
• Compiler: translates code in one language into equivalent in another one.
• Original language → source code.
• Target language → particular machine language.
• Such translation may result in errors.
• Change the code to fix the problem.
• Interpreter: executes the program and evaluates results.
Yes Yes
Edit & Save Compile No Interpret No
Error? Error? DONE
Program Program Program
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 23
PYTHON Programs
• PYTHON program: is a sequence of definitions and commands:
• Definitions are evaluated.
• Commands are executed by PYTHON interpreter in a shell.
• Program is:
1. Compiled: translated into bytecode and stored in a file with “.pyc” extension.
• Bytecode (as opposed to Machine Language) is not tied to any particular CPU → portability.
2. Interpreted: executed by the PYTHON interpreter.
• Commands (i.e. statements):
• Instruct the interpreter to do something.
• Typed directly in a shell or stored in a file that is read into the shell and evaluated.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 24
Objects
• Programs manipulate data objects.
• Objects are:
• Scalar (a.k.a. atomic): cannot be subdivided.
• Non-scalar: have an internal structure that can be accessed.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 25
Scalar Objects
• int – represents integers (e.g. 7)
• float – represents real numbers, (e.g. 1.47)
• bool – represents Boolean values (e.g. True and False)
• NoneType – special type that has only one value, None
• Use the command type() to return the type of an object.
• Example:
PYTHON >>> type(5) What is typed at the prompt in a PYTHON shell.
Prompt
int What appears when the ENTER key is pressed.
>>> type(3.0)
float
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 26
Type Casting
• Convert an object from one type to another type.
• Examples:
• float(3) converts the integer 3 into a float 3.0
• int(3.9) truncates the float 3.9 so it becomes the integer 3
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 27
Printing To Console
• To show code output to a user, use the print command.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 28
Expressions
• Combine objects and operators to form expressions.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 29
Operators on int and float Objects
• i+j → the sum
If both i and j are int the result is int.
• i-j → the difference If either one or both of i and j are float the result is float.
• i*j → the product
• i/j → the division The result is always float.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 30
Binding Variables And Values
• The equal sign (=) is an assignment operator:
• It assigns a value to a variable name (i.e. binds the name to the value).
• Value is stored in the computer’s memory.
• Retrieve the value associated with the variable by invoking its name.
• Example:
ble e
varia valu
>>> pi = 3.14159
>>> pi_approx = 22/7
>>> pi
3.14159
>>> pi_approx.
3.142857142857143
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 31
Abstracting Expressions
• Q: Why give names to values of expressions?
• A: to reuse names instead of the values (easier to change code later)
• Example:
>>> pi = 3.14159
>>> radius = 2.2
>>> area = pi*(radius**2)
>>> area
15.205295600000001
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 32
Progamming V.S. Math
• In programming, one does not “solve for x”
lu e.
a va
to
ed
uat l is:
• Example: e nt: t, eva 1
g n m righ . us+
i t i
n ass n the he lef rad
u g h a io n o o n t ius=
o s e ad = 1
Thr xpres e nam r
E abl n for s +
>>> pi = 3.14159 •
Var
i
re ssio adiu
•
t e xp r
>>> radius = 2.2 n
i vale
Equ
>>> area = pi*(radius**2)
>>> radius = radius+1
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 33
Changing Binding
• Possible to re-bind variable names using new assignment statements.
• Previous value may still be stored in memory but lost its handle:
• It is identical to garbage that is no longer accessible.
• Previous Example:
>>> pi = 3.14159
>>> radius = 2.2
>>> area = pi*(radius**2)
>>> radius = radius+1
• Value of area does not change unless computer is
instructed to recompute it again.
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 34
Sunday, January 13, 2019 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 35