Programming Fundamentals
(SWE – 102)
Introduction
Things you need to know …
Instructor:
Engr. Sonish Aslam
Office:
CS-03
Email:
Student Consultation:
Take appointment before meeting, preferably via
email
2
Things you need to know …
Textbook:
Introduction to Programming using Python
Y. Daniel Liang
Reference Books:
Python Crash Course
Eric Matthes
Head First Python
Paul Barry
Online Sources:
https://realpython.com/
many many more … 3
Marks Distribution
Theory
Quizes 10 Marks
Assignments 10 Marks
Midterm Examination 30 Marks
Final Examination 50 Marks
Total 100 Marks
Laboratory
Quizes / Assignments / Lab 15 Marks
File
Project 15 Marks
Final Exam 20 Marks
Total 50 Marks
4
Things you need to know …
The lecture slides provide only the outline of the
lecture.
These outlines are not a substitute for class attendance
and note taking.
More importantly, these outlines are not a substitute
for the textbook.
5
What is a Computer?
A computer consists of a CPU, memory, hard
disk, monitor, printer, and communication
devices.
Bus
Storage Communication Input Output
Devices Memory CPU Devices Devices Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer
6
CPU
The central processing unit (CPU) is the brain of a
computer. It retrieves instructions from memory and
executes them.
Bus
Storage Communication Input Output
Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer
7
Memory
Memory is to store data and program instructions for CPU to
execute.
A memory unit is an ordered sequence of bytes, each holds 8 bits.
A program and its data must be brought to memory before they
can be executed.
A memory byte is never empty, but its initial content may be
meaningless to your program. The current content of a memory
byte is lost whenever new information is placed in it.
Bus
Storage Communication Input Output
Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer
8
How Data is Stored?
Data of various kinds, are encoded as a
series of bits (zeros and ones). Memory address Memory content
The programmers need not to be
. .
concerned about the encoding and . .
decoding of data, which is performed . .
automatically by the system based on the2000 01001010 Encoding for character ‘J’
encoding scheme. 2001 01100001 Encoding for character ‘a’
2002 01110110 Encoding for character ‘v’
A small number such as three can be 2003 01100001 Encoding for character ‘a’
stored in a single byte. If computer needs2004 00000011 Encoding for number 3
to store a large number that cannot fit into
a single byte, it uses a number of adjacent
bytes.
No two data can share or split a same
byte. A byte is the minimum storage unit.
9
Storage Devices
Memory is volatile, because information is lost
when the power is off.
Programs and data are permanently stored on
storage devices and are moved to memory when
the computer actually uses them.
Bus
Storage Communication Input Output
Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer
10
Output Devices: Monitor
The monitor displays information (text and
graphics).
The resolution and dot pitch determine the
quality of the display.
Bus
Storage Communication Input Output
Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer
11
Monitor Resolution and Dot Pitch
Resolution
The resolution specifies the number of pixels per square
inch.
Pixels (short for “picture elements”) are tiny dots that
form an image on the screen.
The higher the resolution, the sharper and clearer the
image is.
PC monitors are usually 15-inch, 17-inch, 19-inch, or 21-
inch.
Dot Pitch
The dot pitch is the amount of space between pixels.
The smaller the dot pitch, the better the display.
12
Communication Devices
Network interface card (NIC) is a device to
connect a computer to a local area network.
The LAN is commonly used in business,
universities, and government organizations.
A DSL (digital subscriber line) uses a phone
line to transfer data.
Bus
Storage Communication Input Output
Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer
13
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.
14
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
15
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
…
Assembler …
ADDF3 R1, R2, R3
1101101010011010
…
…
16
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;
17
Popular High-Level Languages
COBOL (COmmon Business Oriented Language)
FORTRAN (FORmula TRANslation)
BASIC (Beginner All-purpose Symbolic Instructional
Code)
Pascal (named for Blaise Pascal)
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 this course)
18
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 19
Generations of Programming Language
Thefirst generation languages, or 1GL, are
low-level languages that are machine language.
The second generation languages, or 2GL, are
also low-level languages that generally consist
of assembly languages.
The third generation languages, or 3GL, are
high-level languages such as C.
20
Generations of Programming Language
The fourth generation languages, or 4GL, are
languages that consist of statements similar to
statements in a human language. Fourth
generation languages are commonly used in
database programming and scripts.
The fifth generation languages, or 5GL, are
programming languages that contain visual
tools to help develop a program. A good
example of a fifth generation language is
Visual Basic.
21
Programming as Problem Solving
Developing
Problem solving
a Program:
principles:
Analyze the problem
Design
Completely understand
the program
the
Codeproblem
the program
Devise
Test theaprogram
plan to solve it
Carry out the plan
Review the results
1) Analyze the Problem
Brewster’s Thousands
The problem: Brewster wants to invest money at a
local bank. There are many options such as interest
rates, terms of deposit, compounding frequencies.
He needs a program to compute, for any given
initial investment, the final maturity (value) of the
deposit.
What are the inputs? (given data)
What are the outputs? (required data)
How will we calculate the required outputs from the given
inputs?
2) Design the Program
Create an outline of the program
An algorithm – a step by step procedure that
will provide the required results from the given
inputs.
Algorithm Examples: Instructions on how to
make a cake, use the bank’s ATM, etc.
3) Code the Program
Once the design is completed, write the
program code.
Code is written in some programming language
such as BASIC, Pascal, C++, Java, etc.
In this course we write code in pseudo-code,
developing the skills to be used when studying
the specific languages.
4) Testing the program
Locate any errors (bugs)
Testing is done throughout the development cycle
Desk-checking, or code walkthrough is performed to
locate errors in the code.
Pretend you are the computer and execute your own code.
Ultimate test is to run the program to see if the outputs
are correct for the given inputs.
Operating Systems
The operating system (OS) is a
program that manages and User
controls a computer’s
activities e.g. Windows 10. Application Programs
Windows is currently the most
popular PC operating system. Operating System
Application programs such as
an Internet browser and a Hardware
word processor cannot run
without an operating system.
27
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 is now used in Google search engine, in
mission critical projects in NASA, in processing
financial transactions at New York Stock Exchange.
28
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.
29
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.
30
Python is Dynamic Language
Python is a Dynamic Language: Python is a
dynamic language for the beginner to advance level
programmers and supports the development of a
wide range of applications include
Simple text processing applications
Functional applications
Object-Oriented base applications
Web applications (flask and django)
GUI base applications
Artificial Intelligence base applications
Interactive Gaming programming. 31
Python’s History
Created by Guido van Rossum in Netherlands
in 1990
Open source
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.
32
Popular Python IDEs
For Beginners
IDLE
Thonny
For Professional Developers
Spyder
Jupyter
PyCharm
Visual Studio
We will use Thonny in our course…
33
Thonny Environment
34
Run Python Program
RQ 35
Anatomy of a Python Program
Statements
Comments
Indentation
36
Statement
A statement represents an action or a sequence
of actions.
The statement print("Welcome to Python") is a
statement to display the greeting "Welcome to
Python“.
# Display two messages
print("Welcome to Python")
print("Python is fun")
37
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")
38
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.
39
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.
40
Programming Errors
Syntax Errors
Error in code construction
Runtime Errors
Causes the program to abort
Logic Errors
Produces incorrect result
41
Summary
Computer
Programming Languages
Python Language
Programming with Python
42