Week 02 - Introduction To LAB and Programming Concepts
Week 02 - Introduction To LAB and Programming Concepts
3
Program and Programming
Computer Algorithms
An algorithm is a finite number of clearly Euclid’s Algorithm for computing the
described, unambiguous “doable” steps that greatest common denominator (GCD)
can be systematically followed to produce a of two given integers.
desired result for given input in a finite
amount of time (that is, it eventually 1. Assign M the value of the larger of
terminates). the two values.
2. Divide M by N, call the remainder R.
The word “algorithm” is derived from the
3.If R is not 0, then assign M the value
ninth-century mathematician, Al- of N, assign N the value of R, and go to
Khwarizmi who worked on “written step 2.
processes to achieve some goal.” 4. The greatest common divisor is N.
Syntax and Semantics
Syntax and semantics are important concepts that apply to all
languages.
The syntax of a language is a set of characters and the acceptable sequences
(arrangements) of those characters.
English, for example, includes the letters of the alphabet, punctuation, and properly
spelled words and properly punctuated sentences. “Hello there, hao ar you?”
The semantics of a language is the meaning associated with each syntactically correct
sequence of characters.
“Colorless green ideas sleep furiously.”
Every language has its own syntax and semantics. This sentence is syntactically correct, but
has no meaning. Thus, it is semantically
incorrect.
7
Program Translation
A central processing unit (CPU) is designed to interpret and execute
a specific set of instructions represented in binary form (i. E., 1s and
0s) called machine code.
Only programs in machine code can be executed by a CPU.
8
Writing programs at this “low level” is tedious and e r r o r - p r o n e .
Therefore, most programs are written in a “ hi g h- l e v e l ” programming
language such as Python.
Since the instructions of such programs are not in machine code that
a CPU can execute, a translator program must be used.
9
Compiler
10
Interpreter
An interpreter can immediately execute instructions as they are entered. This is referred
to as interactive mode. This is a very useful feature for program development. Python, as
we shall see, is executed by an interpreter.
11
Program Debugging: Syntax Errors vs. Semantic Errors
Syntax errors are caused by invalid syntax (for example, entering prnt
instead of print).
12
Semantic Errors
In contrast, semantic errors (generally called logic errors ) are errors in program logic.
Such errors cannot be automatically detected, since translators cannot understand the
intent of a given computation. Therefore, programs are not terminated when containing
logic errors, but give incorrect results.
a translator would have no means of determining that the divisor should be 3 and not 2.
Computers do not understand what a program is meant to do, they only follow the
instructions given. It is up to the programmer to detect such errors.
Program debugging is not a trivial task, and constitutes much of the time of program
development.
13
How not to Program
Designing a Program
Programs must be designed before they are written
Program development cycle:
◦Design the program
◦Write the code
◦Correct syntax errors
◦Test the program
◦Correct logic errors
Interpreted Language
Python instructions can be executed interactively
21
Using Python
Python must be installed and configured prior to use
◦One of the items installed is the Python interpreter
Python interpreter can be used in two modes:
◦Interactive mode: enter statements on keyboard
◦Script mode: save statements in Python script
Interactive Mode
When you start Python in interactive mode, you will see a
prompt
◦Indicates the interpreter is waiting for a Python statement to
be typed
◦Prompt reappears after previous statement is executed
◦Error message displayed If you incorrectly type a statement
Good way to learn new parts of Python
Writing Python Programs and Running Them in
Script Mode
Statements entered in interactive mode are not saved as a
program
To have a program use script mode
◦Save a set of Python statements in a file
◦The filename should have the .py extension
◦To run the file, or script, type
python filename
at the operating system command line
The IDLE Programming Environment
IDLE (Integrated Development Program): single program that
provides tools to write, execute and test a program
◦Automatically installed when Python language is installed
◦Runs in interactive mode
◦Has built-in text editor with features designed to help write
Python programs
The IDLE Python Development Environment
IDLE is an integrated development environment (IDE). An IDE is a bundled set of software tools
for program development. This typically includes,
• an editor
for creating and modifying programs
• a translator
for executing programs, and
• a program debugger
for taking control of the execution of a program to aid in finding
program errors
22
Using the Python Interpreter
Using IDLE
◦Editing programs
◦Running programs
Programming/Python concepts
◦Variables
◦Numbers
◦Strings
◦Getting input
◦Comments
IDLE
There should be a link on your desktop
If not, go to the Start menu, scroll until you find Python, and then in its submenu
choose IDLE.
At the interpreter in the Shell, we can start typing lines of Python code
immediately
◦5 + 8
◦18 * 11
◦13 / 9
◦13 // 9
◦13 % 3 % modulo arithmetic
Writing more than one line of code at a time
Use the editor
In IDLE, choose File – New File
Enter some code
On Windows, if you are viewing a directory with Windows Explorer, you can Shift-Right click on
the window, and select “Open Command Window Here”.
To run a Python file, type
◦python NameOfFile.py
Displaying Output with the print Function
print(13)
print(“hello world”)
print(“hello world”, 5)
print(‘Your task is to read ”Hamlet” by next week‘)
Try this:
◦ print(hello world)
Comments