You are on page 1of 37

CME1203 Introduction to

Computer Engineering - Lab


WEEK 2
INTRODUCTION TO LAB AND PROGRAMMING CONCEPTS
Computational
Problem Solving
Steps

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.

There are two fundamental types of translators:

• Compiler software that translates programs into machine code to


be executed by the CPU

• Interpreter software that executes programs in place of the CPU

9
Compiler

Compiled programs generally execute faster than interpreted programs.

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

Program debugging is the process of finding and correcting errors


(“bugs”) in a computer program. Programming errors are inevitable during
program development.

Syntax errors are caused by invalid syntax (for example, entering prnt
instead of print).

Since a translator cannot understand instructions containing syntax


errors, translators terminate when encountering such errors indicating
where in the program the problem occurred.

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.

For example, if a program computed the average of three numbers as follows,


(num1 + num2 + num3) / 2.0

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

Determine the steps that must be taken to perform the task


◦Break down required task into a series of steps
◦Create an algorithm, listing logical steps that must be taken
Pseudocode Flowcharts
Pseudocode: fake code Flowchart: diagram that graphically depicts the
◦Informal language that has no syntax steps in a program
rule ◦Ovals are terminal symbols
◦Not meant to be compiled or executed ◦Parallelograms are input and output
◦Used to create model program symbols
◦No need to worry about syntax errors, ◦Rectangles are processing symbols
can focus on program’s design ◦Symbols are connected by arrows that
◦ Can be translated directly into actual represent the flow of the program
code in any programming language
Input, Processing, and Output

Typically, computer performs three-step process


◦Receive input
◦Input: any data that the program receives while it is running
◦Perform some process on the input
◦Example: mathematical calculation
◦Produce output
The Python Programming Language
The Python Programming Language was created by Guido van Rossum. It was
first released in the early 1990s.
Its name comes from a 1970s British comedy sketch show called Monty Python’s
Flying Circus. (An example of their work is The Argument Clinic).
Companies and organizations that use Python include YouTube, Google, Yahoo
and NASA.
Python is completely free to use and distribute.
Python is commonly applied in many domains: System programming, game
programming, natural language analysis, machine learning and data mining,
image processing, network programming etc.
Python Features
Simple Syntax
Python programs are clear and easy to read

Interpreted Language
Python instructions can be executed interactively

Powerful Programming Features


Can accomplish significant computation with few instructions

Numerous Python Modules Provide Additional Capabilities

Multiple Paradigms: Functional, Imperative, Object Oriented

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

name = input("What is your name? ")


age = int(input("How old are you? "))
print(name, "is", age, "years old.")

Choose Run – Run Module


◦If you’ve never saved, it will prompt you to do so
◦You can put it where you want it, with a name of your choosing
◦The extension will be .py
How to run at the command prompt
You do not have to use IDLE to run your code
If you create a command prompt window, you can run from there
In Windows, the command prompt is “CMD” or you will find it in your list of programs
“Command Prompt”
◦On the mac, this is your terminal window.

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

Function: piece of prewritten code that performs an


operation
print function: displays output on the screen
Argument: data given to a function
◦Example: data that is printed to screen
Statements in a program execute in the order that they
appear
◦From top to bottom
Strings and String Literals

String: sequence of characters that is used as data


String literal: string that appears in actual code of a
program
◦Must be enclosed in single (‘) or double (“) quote
marks
◦String literal can be enclosed in triple quotes (''' or
""")
◦Enclosed string can contain both single and double quotes and can
have multiple lines
Displaying Multiple Items with the print
Function
Python allows one to display multiple items with a single call to
print
◦Items are separated by commas when passed as arguments
◦Arguments displayed in the order they are passed to the function
◦Items are automatically separated by a space when displayed on screen
Printing

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

Comments: notes of explanation within a program


◦Ignored by Python interpreter
◦Intended for a person reading the program’s code
◦Begin with a # character
End-line comment: appears at the end of a line of code
◦Typically explains the purpose of that line
Comments – Code Sample
Variables
Variable: name that represents a value stored in the computer memory
◦Used to access and manipulate data stored in memory
◦A variable references the value it represents

Assignment statement: used to create a variable and make it reference data


◦General format is variable = expression
◦Example: age = 29
◦ Assignment operator: the equal sign (=)

In assignment statement, variable receiving value must be on left side


A variable can be passed as an argument to a function
◦Variable name should not be enclosed in quote marks

You can only use a variable if a value is assigned to it


Using Variables
x = 13
y = 23
print(x)
print(x + y)
name = “Ali”
print(name)
print(name, y)
Try this:
◦ print(name + y)
Variables – Code Sample
Getting input from the user
t = input("Type something: “)
print(t)

You might also like