Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Programming Lecture 1

This document serves as an introduction to computers, programming, and Python, detailing the grading structure, textbooks, and essential programming concepts. It outlines the programming process in five main steps: defining the problem, planning the solution, coding, testing, and documenting the program, as well as discussing programming languages and constructs. The document emphasizes the importance of structured programming and provides insights into procedural, non-procedural, and object-oriented programming languages.

Uploaded by

sagar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Programming Lecture 1

This document serves as an introduction to computers, programming, and Python, detailing the grading structure, textbooks, and essential programming concepts. It outlines the programming process in five main steps: defining the problem, planning the solution, coding, testing, and documenting the program, as well as discussing programming languages and constructs. The document emphasizes the importance of structured programming and provides insights into procedural, non-procedural, and object-oriented programming languages.

Uploaded by

sagar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Chapter 1 Introduction to Computers,

Programs, and Python


APPLIED PROGRAMMING

Course Supervisor: Syed Muhammad Hassan

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.

You tell a computer what to do through programs. Without


programs, a computer is an empty machine. Computers do
not understand human languages, so you need to use
computer languages to communicate with them.

Programs are written using programming languages.

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.

a.Top-down program design (Modularization): It is a type of Modular


Programming Approach. Once you have determined the outputs and
inputs, you can use top-down program design or top-down
decomposition to identify the program’s processing steps. Such
steps are called program modules. Each module is made up of
logically related program statements. Procedural programming
follows this approach.
7
Top-down program design
Fig 1.1 shows an example of top down
program design for a payroll report. Each of
boxes shown is a module or, in the lower
part, a sub-module.
For example: Given a list of students’ test
scores, find the highest and lowest score and
the average score. Sub-problem 1: read
students’ scores. Sub-problem 2: find highest
score. Sub-problem 3: find lowest score.
Sub-problem 1 can be considered as one
action and therefore needs no further
refinement. Sub-problems 2 and 3 however
can be further divided into a group of
actions.

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

1.Start Get 2 numbers


2.Get two numbers
A+B
3.Add them (A + B)
4.Print the answer
5.End Print answer

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

Fig 1.2: Flowchart Symbols 13


Fig: 1.4: Flowchart for reading temperature

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).

1. Sequence Control Structure (Sequence Logic or Sequential Flow):


If nothing is specified then the modules are executed in the sequence in which
they are written. Thus in it the modules are executed one after the other.
The sequence control structure is shown below:
Algorithm
.
.
.
Module A
Module B
Module C
.
. 19
2. Selection Control Structure (Selection Logic or Conditional Flow):
In it there are various conditions on the basis of which one module is
selected out of several given modules. The structures which
implement this logic are called conditional structures. The
conditional structures can be divided into following categories:

1. Single Alternative: This structure has the following form:


If condition, then
[Module A]
[End of If structure]

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:

2. Double Alternative: This structure has the


following form:
If condition, then
[Module A]
Else
[Module B]
[End of If structure]

According to this if the condition is true then


module A is executed. Otherwise, module B
is executed. 21
3. Multiple Alternative: This structure has the following form:
If condition (1), then
[Module A1]
Else If condition (2), then
[Module A2]
.
.
.
Else If condition (n), then
[Module An]
Else
[Module B]
[End of If structure]
22
Multiple Alternative (cont.)
According to this only one of the modules will be executed. If condition 1 is true then module A1 will be executed.
Otherwise condition 2 will be checked. If condition 2 is true then module A2 will be executed. And so on. If none of
the conditions is true then module B will be executed. 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:

Repeat steps 1 to n while condition:


[Steps]
[End of Repeat-while structure]

Its flowchart is:

24
Programming Languages
Machine Language Assembly Language High-Level Language

Machine language is a set of primitive instructions


built into every computer. The instructions are in
the form of binary code, so you have to enter binary
codes for various instructions. Program with native
machine language is a tedious process. Moreover
the programs are highly difficult to read and
modify. For example, to add two numbers, you
might write an instruction in binary like this:

1101101010011010
25
Programming Languages
Machine Language Assembly Language High-Level Language

Assembly languages were developed to make


programming easy. Since the computer cannot understand
assembly language, however, a program called assembler is
used to convert assembly language programs into machine
code. For example, to add two numbers, you might write an
instruction in assembly code like this:
ADDF3 R1, R2, R3
Assembly Source File
Machine Code File


ADDF3 R1, R2, R3
Assembler …
1101101010011010

26
Programming Languages
Machine Language Assembly Language High-Level Language

The high-level languages are English-like and easy to learn


and program. For example, the following is a high-level
language statement that computes the area of a circle with
radius 5:
area = 5 * 5 * 3.1415;

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;
}}

Low-level program LOAD


LOAD r1,b
r1,b
LOAD
LOAD r2,h
r2,h
MUL
MUL r1,r2
r1,r2
DIV
DIV r1,#2
r1,#2
RET
RET

Executable Machine code 0001001001000101001001


0001001001000101001001
001110110010101101001.
001110110010101101001.
..
..
30
Procedural programming
languages
• Procedural language is a type of computer programming language that
specifies a series of well-structured steps and procedures within its
programming context to compose a program. It contains a systematic
order of statements, functions and commands to complete a
computational task or program.
• Procedural language is also known as imperative language.
• Procedural languages are:
• C/C++ Language
• FORTRAN (FORmula TRANslation)
• BASIC (Beginners All Purpose Symbolic Instruction Code)
• COBOL (Common Business Oriented Language) These types of
languages are “Third-Generation Language”.
31
Non Procedural Languages
• In non-procedural languages the computer is not limited to a set of
precise instructions. Instead, the programmer defines only the
problem—not the instructions--to solve the problem. Non Procedural
Programming Languages are:
• SQL (Structured Query Language)
• LISP (List Processing)
• PROLOG (PROgramming with LOGic)

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.

Source File Compiler Machine-language


Linker Executable File
File

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

Python 2 vs. Python 3


Python 3 is a newer version, but it is not
backward compatible with Python 2. That
means if you write a program using Python
2, it may not work on Python 3.

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

# Display two messages


print("Welcome to Python")
print("Python is fun")

45
Trace a Program Execution
Execute a statement

# Display two messages


print("Welcome to Python")
print("Python is fun")

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“.

# Display two messages


print("Welcome to Python")
print("Python is fun")

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:

# Display two messages


print("Welcome to Python")
print("Python is fun")

49
Special Symbols

Character Name Description

() Opening and closing Used with functions.


parentheses
# Pound sign Precedes a comment line.

" " 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.

Include your name, class section, instructor, date, and a


brief description at the beginning of the program.

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

You might also like