Programming Lecture 1
Programming Lecture 1
1
Grading
• Quizzes = 10%
• Assignments = 10%
• Lab = 15%
• Class Participation = 5%
• Midterm = 20%
• Final Exam = 40%
2
Textbook:
• Programming in Python 3: A Complete Introduction to the Python
Language, Second Edition, Mark Summerfield, 2010.
Reference Books:
• Introduction to Programming using Python, Y. Daniel Liang, 1st
edition, 2017.
• Introduction to Computer Science Using Python: A Computational
Problem-Solving Focus by Charles Dierbach, Wiley 2013.
• Automate the Boring Stuff with Python by Al Sweigart, 2015.
• Python for Everybody by Charles R. Severance.
3
Programs
Computer programs, known as software, are instructions
to the computer.
4
Characteristics of successful
computer program
• Correct: it does what is supposed to do
• Usable: it is easy to use
• Reliable: it works without failing
• Understandable: it can be easily comprehended
• Modifiable: it can be enhanced and updated
• Maintainable: it can be corrected when errors are found
• Flexible: it can be modified to supply other information as required
• General: it is not too specialized
• Efficient: it does not waste computer resources
5
Five main steps in programming
process
1.Defining the problem: Suppose that, as a programmer, you are
contacted because your services are needed. You meet with users
from the client organization to analyze the problem, or you meet
with a systems analyst who outlines the project. Specifically, the
task of defining the problem consists of :
• determining program objectives,
• identifying what it is you know (input-given data),
• what it is you want to obtain (output-the result),
• determining processing requirements.
• Eventually, you produce a written agreement that, among other
things, specifies the kind of input, processing, and output required.
This is not a simple process.
6
Five main steps in
programming process
2.Planning the solution: Once the programmer has determined what
the program must do, the next step is to plan a solution, preferably
using structured programming techniques or program design tools.
Three common ways of planning the solution to a problem are top-
down program design, to write algorithm and to draw a flowchart.
Fig 1.1
Bottom-up technique:
8
Five main steps in programming
process
b. Algorithm:
• It is a step by step method of designing a program using normal
human language statements.
• It describes the logic and processing flow of a program.
• Algorithm is like an outline or summery form of the program you will
write.
9
Sample Algorithm
• Task: add two numbers
• Algorithm :
1. Start
2. Get two numbers
3. Add them (a + b)
4. Print the answer
5. End
10
What does a flowchart look
like?
• Algorithm : Start
End
11
Five main steps in programming
process
c. Flowchart:
• Flowchart is a pictorial representation of a step-by-step solution to a
problem.
• It consists of arrows representing the direction the program takes and
boxes and other symbols representing actions.
• It is a map of what your program is going to do and how it is going to
do it.
• The American National Standards Institute (ANSI) has developed a
standard set of flowchart symbols. Figure 1.2 shows the symbols and
how they might be used in a simple flowchart of a common everyday
act-preparing a letter for mailing.
12
Flowchart Symbols
14
Fig 1.3: Flowchart for Mailing Letter
Five main steps in
programming process
3.Coding the program:
• As the programmer, your next step is to code the program-that is, to
express your solution in a programming language.
• You will translate the logic from the flowchart or pseudocode-or some
other tool-to a programming language.
4.Testing the program: Eventually, after coding the program, you must
prepare to test it on the computer. This step involves these phases:
• Desk-checking. This phase, similar to proof reading, is sometimes avoided
by the programmer who is looking for a shortcut and is eager to run the
program on the computer once it is written. In desk-checking you simply sit
down and mentally trace, or check, the logic of the program to attempt to
ensure that it is error-free and workable.
15
Five main steps in
programming process
• Translating. A translator is a program that checks the syntax of
your program to make sure the programming language was used
correctly, giving you all the syntax-error messages, called
diagnostics, and then translates your program into a form the
computer can understand.
• The translator tells you if you have improperly used the
programming language in some way. These types of mistakes are
called syntax errors.
• The translator produces descriptive error messages. For example,
if you mistakenly write N=2 *(I+J))-which has two closing
parentheses instead of one-you will get a message that says,
"UNMATCHED PARENTHESES.“
16
Five main steps in
programming process
• Programs are most commonly translated by a compiler. A compiler
translates your entire program at one time. The translation involves your
original program, called a source module, which is transformed by a
compiler into an object module.
• Prewritten programs from a system library may be added during the
link/load phase, which results in a load module. The load module can then
be executed by the computer.
• Debugging. A term used extensively in programming, debugging means
detecting, locating, and correcting bugs (mistakes), usually by running the
program. These bugs are logic errors, such as telling a computer to repeat
an operation but not telling it how to stop repeating. In this phase you run
the program using test data that you devise. You must plan the test data
carefully to make sure you test every part of the program.
17
Five main steps in programming
process
5. Documenting the Program: Documentation is a written detailed
description of the programming cycle and specific facts about the
program. Typical program documentation materials include the
origin and nature of the problem, a brief narrative description of
the program, logic tools such as flowcharts and pseudocode,
data-record descriptions, program listings, and testing results.
Comments in the program itself are also considered an essential
part of documentation.
18
Computer Programming Constructs/Control Constructs
Structured Programming Constructs:
1. Sequence Control Structure (Sequence Logic or Sequential Flow).
2. Selection Control Structure (Selection Logic or Conditional Flow).
3. Repetition Control Structure (Iteration Logic or Repetitive Flow).
20
Single Alternative(cont.)
According to this if the condition is true then
module A is executed. Otherwise, module A is
skipped. Its flowchart is:
23
4. Repetition Control Structure (Iteration Logic or Repetitive Flow): In
this structure, loops are implemented. Loop is used to implement
those statements which are to be repeated again and again until
some condition is satisfied. It implements while and for loops. Each
of these begin with Repeat statement. For example is case of Repeat-
while structure:
24
Programming Languages
Machine Language Assembly Language High-Level Language
1101101010011010
25
Programming Languages
Machine Language Assembly Language High-Level Language
…
ADDF3 R1, R2, R3
Assembler …
1101101010011010
…
…
26
Programming Languages
Machine Language Assembly Language High-Level Language
27
Popular High-Level
Languages
•COBOL (COmmon Business Oriented Language)
•FORTRAN (FORmula TRANslation)
•BASIC (Beginner All-purpose Symbolic Instructional Code)
•Pascal (named for Blaise Pascal)
•Ada (named for Ada Lovelace)
•C (whose developer designed B first)
•Visual Basic (Basic-like visual language developed by Microsoft)
•Delphi (Pascal-like visual language developed by Borland)
•C++ (an object-oriented language, based on C)
•C# (a Python-like language developed by Microsoft)
•Python (We use it in the book)
28
Programming Languages
• High level programming languages: is languages program than use
languages or syntax which closes to human languages so; it is easy to
understanding the languages. This type of language is machine-
independent, and uses similar language as English, which is easily
understandable by human.
Types of high level languages are:
1) Procedural Languages
2) Functional & Non procedural Languages
3) Object Oriented Languages
29
Levels of Programming
Languages
High-level program class
class Triangle
Triangle {{
...
...
float
float surface()
surface()
return
return b*h/2;
b*h/2;
}}
32
Object Oriented
•
Programming
Object-oriented programming (OOP) is an engineering approach for
building software systems
• Based on the concepts of classes and objects that are used for modeling the real
world entities
• A program models a world of interacting objects
• Objects create other objects and “send messages” to each other (in Java,
call each other’s methods)
• Each object belongs to a class
• A class defines properties of its objects
• The data type of an object is its class
• Programmers write classes (and reuse existing classes)
• All modern languages are object-oriented: Java, C#, PHP (Hypertext
Preprocessor), Perl (Practical Extraction and Reporting Language), C++, ... 33
Compiling Source Code
A program written in a high-level language is called a
source program. Since a computer cannot understand a
source program. Program called a compiler is used to
translate the source program into a machine language
program called an object program. The object program is
often then linked with other supporting library code before
the object can be executed on the machine.
Library Code
34
Operating Systems
The operating system (OS) is
a program that manages and User
controls a computer’s
activities. You are probably Application Programs
using Windows 98, NT, 2000,
XP, or ME Etc. Windows is
currently the most popular Operating System
PC operating system.
Application programs such as Hardware
an Internet browser and a
word processor cannot run
without an operating system.
35
What is Python?
General Purpose Interpreted Object-Oriented
Python is a general purpose programming
language. That means you can use Python to write
code for any programming tasks. Python are now
used in Google search engine, in mission critical
projects in NASA, in processing financial
transactions at New York Stock Exchange.
36
What is Python?
General Purpose Interpreted Object-Oriented
Python is interpreted, which means that python
code is translated and executed by an interpreter
one statement at a time. In a compiled language, the
entire source code is compiled and then executed
altogether.
37
What is Python?
General Purpose Interpreted Object-Oriented
Python is an object-oriented programming
language. Data in Python are objects created from
classes. A class is essentially a type that defines the
objects of the same kind with properties and
methods for manipulating objects. Object-oriented
programming is a powerful tool for developing
reusable software.
38
Python’s History
• created by Guido van Rossum in Netherlands in 1990
• Open source
39
Launch Python
40
Launch Python IDLE
41
Run Python Script
42
A Simple Python Program
Listing 1.1
# Display two messages
print("Welcome to Python")
print("Python is fun")
IMPORTANT NOTE:
(1) To enable the buttons, you must download the entire
Welcome slide file slide.zip and unzip the files into a directory (e.g.,
c:\slide). (2) You must have installed Python and set python
bin directory in the environment path. (3) If you are using
Run Office 2010, check PowerPoint2010.doc located in the
same folder with this ppt file.
43
Creating and Editing Using Notepad
To use Notepad, type
notepad Welcome.py
from the DOS prompt.
44
Trace a Program Execution
Execute a statement
45
Trace a Program Execution
Execute a statement
46
Anatomy of a Python
Program
• Statements
• Comments
• Indentation
47
Statement
A statement represents an action or a sequence of
actions. The statement print("Welcome to Python") in
the program in Listing 1.1 is a statement to display the
greeting "Welcome to Python“.
48
Indentation
The indentation matters in Python. Note that the
statements are entered from the first column in the new
line. It would cause an error if the program is typed as
follows:
49
Special Symbols
" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
''' ''' Opening and closing Enclosing a paragraph comment.
quotation marks
50
Programming Style and
Documentation
• Appropriate Comments
• Proper Indentation and Spacing
Lines
51
Appropriate Comments
Include a summary at the beginning of the program to
explain what the program does, its key features, its
supporting data structures, and any unique techniques it
uses.
52
Proper Indentation and
Spacing
• Indentation
• Indent four spaces.
• A consistent spacing style makes programs clear and
easy to read, debug, and maintain.
• Spacing
• Use blank line to separate segments of the code.
53
Programming Errors
• Syntax Errors
• Error in code construction
• Runtime Errors
• Causes the program to abort
• Logic Errors
• Produces incorrect result
54