Chapter 1
Introduction to Computers,
Programs, and Python
CPIT 110 (Problem-Solving and Programming)
Modified by Ahmad Khoj
Introduction to Programming Using Python, By: Y. Daniel Liang
Version 2.0
What is a Computer?
• A computer is an electronic device that stores and processes data.
• A computer includes both hardware and software.
• A computer consists of: CPU, memory, storage devices, input
devices, output devices, and communication devices.
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
1.2 2
CPU
• The central processing unit (CPU) is the brain of a computer.
• It retrieves instructions from memory and executes them.
• The CPU speed is measured in megahertz (MHz).
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
1.2 3
Memory
• Memory is to store data and program instructions for CPU to execute.
• A memory unit is an ordered sequence of bytes, each holds eight bits. (a
bit = 0 or 1)
• A program and its data must be brought to memory before they can be
executed.
• The current content of a memory byte is lost whenever new information is
placed in it.
• Memory is volatile, because information is lost when the power is off.
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
1.2 4
Storage Devices
• Programs and data are permanently stored on storage devices and
are moved to memory when the computer actually uses them.
• There are three main types of storage devices: Disk drives (hard
disks and floppy disks), CD drives (CD-R and CD-RW), and Tape drives
(magnetic tape).
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
1.2 5
Output Devices
• An output device is any device used to send data from a computer
to another device or user. .
• 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
1.2 6
Input Devices
• An input device is any hardware device that sends data to a
computer.
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
1.2 7
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.
1.3 8
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
1.3 9
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.
• Writing code in assembly language is easier than in machine
language. However, it is still tedious to write code in assembly
language.
• For example, to add two numbers, you might write an instruction in
assembly code like this:
ADD 2, 3, result
1.3 10
Programming Languages
Machine Language Assembly Language High-Level Language
• The high-level languages are English-like and easy to learn and
program.
• Since the computer cannot understand high-level languages,
however, a program called interpreter or compiler is used to convert
high-level language programs into machine code.
• For example, the following is a high-level language statement
(instruction) that computes the area of a circle with radius 5:
area = 5 * 5 * 3.1415
1.3 11
Popular High-Level Languages
1.3 12
Interpreting vs Compiling Source Code
• An interpreter reads one statement from the source code, translates
it to the machine code or virtual machine code, and then executes it
right away.
• A compiler translates the entire source code into a machine-code
file, and the machine-code file is then executed altogether.
1.3 13
Operating Systems
• The operating system (OS) is a
program that manages and
controls a computer’s activities.
• You are probably using Windows
10, Linux, or macOS.
• Windows is currently the most
popular PC operating system.
• Application programs - such as
an Internet browser and a word
processor - cannot run without
an operating system.
1.4 14
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.
1.5 15
What is Python?
General Purpose Interpreted Object-Oriented
• Python is interpreted.
• Which means that python code is translated and executed one
statement at a time by an interpreter.
1.5 16
What is Python?
General Purpose Interpreted Object-Oriented
• Python is an object-oriented programming language.
• Data in Python are objects created from classes.
1.5 17
Python’s History
• Python is open source.
• Open-source software is a type of computer software in which
source code is released under a license in which the copyright
holder grants users the rights to study, change, and distribute the
software to anyone and for any purpose.
1.5 18
Install Python
• Go to [Link]/downloads and then download and install
the last version of Python 3.7.x for your operating system.
1.6 19
Install PyCharm
• Go to [Link] and then
download and install “Community” version.
1.6 20
Welcome with Two Messages
Program 1
Write a program that displays Welcome to Python and Programming is
fun. The output should be as the following:
Welcome to Python
Python is fun
The Solution:
LISTING 1.1 [Link]
1 # Display two messages
2 print("Welcome to Python")
3 print("Python is fun")
A Simple Python Program Program 1 21
Code Tracing
Code tracing is when the programmer interprets the results of each line of code
and keeps track of the effect of each statement.
LISTING 1.1 [Link]
1 # Display two messages
2 print("Welcome to Python") It is a comment, so do
nothing.
3 print("Python is fun")
A Simple Python Program 1 of 3 Program 1 22
Code Tracing
LISTING 1.1 [Link]
1 # Display two messages
2 print("Welcome to Python") Execute a statement
3 print("Python is fun")
The output of the
Welcome to Python statement
A Simple Python Program 2 of 3 Program 1 23
Code Tracing
LISTING 1.1 [Link]
1 # Display two messages
2 print("Welcome to Python") Execute a statement
3 print("Python is fun")
Welcome to Python The output of the
statement
Python is fun
A Simple Python Program 3 of 3 Program 1 24
Compute an Expression
Program 2
10.5 + 2 × 3
Write a program that evaluates and print its result.
45 − 3.5
The Solution:
LISTING 1.3 [Link]
1 # Compute expression
2 print( (10.5 + 2 * 3) / (45 - 3.5) )
The output:
0.39759036144578314
Simple Examples Program 3 25
Statement
• A statement represents an action or a sequence of actions.
LISTING 1.1 [Link]
1 # Display two messages It is a statement
(action)
2 print("Welcome to Python")
3 print("Python is fun") It is a statement
(action)
Anatomy of a Python Program 26
Note
• Note that the indentation matters in Python. A wrong indentation
cause an error.
• Don’t put any punctuation at the end of a statement.
Identify and fix the errors in the following code:
1 ..print("Welcome to Python")
2 print("Python is fun").
Solution:
The errors are the incorrect indentation in line 1 and the punctuation (.)
in line 2.
1 print("Welcome to Python")
2 print("Python is fun")
Simple Examples 27
Note
• Python programs are case sensitive.
• It would be wrong, for example, to replace print in the program
with Print.
1 # Display two messages
2 print("Welcome to Python")
3 Print("Python is fun")
Anatomy of a Python Program 28
Comment
• Comments help programmers communicate and understand a program.
• They are not programming statements and thus are ignored by the
interpreter.
• In Python, comments are preceded by a pound sign (#) on a line, called a
line comment, or enclosed between three consecutive single quotation
marks (''') on one or several lines, called a paragraph comment.
1 # This program displays Welcome to Python (a line comment)
2 ''' This program displays Welcome to Python and
3 Python is fun (a paragraph comment)
4 '''
5 print("Welcome to Python")
6 print("Python is fun")
Anatomy of a Python Program 29
Special Symbols
Anatomy of a Python Program 30
Types of Programming Errors
• Programming errors can be categorized into three types:
◦ Syntax Errors
Error in code construction.
◦ Runtime Errors
Causes the program to abort.
◦ Logic Errors
Produces incorrect result.
1.8 31
Syntax Errors
• Syntax errors result from errors in code construction, such as
mistyping a statement, incorrect indentation, omitting some
necessary punctuation, or using an opening parenthesis without a
corresponding closing parenthesis.
• Python has its own syntax, and you need to write code that obeys
the syntax rules. If your program violates the rules Python will report
syntax errors.
1.8 32
Runtime Errors
• Runtime errors are errors that cause a program to terminate abnormally.
• They occur while a program is running if the Python interpreter detects an operation that is impossible to carry out.
• Input mistakes typically cause runtime errors.
• An input error occurs when the user enters a value that the program cannot handle.
• For instance, if the program expects to read in a number, but instead the user enters a string of text, this causes data-
type errors to occur in the program.
1.8 33
Logic Errors
• Logic errors occur when a program does not perform the way it was
intended to.
• Logic errors produce unintended, incorrect or undesired output or
other behavior, although it may not immediately be recognized as
such.
• In fact, they do not cause the program to terminate abnormally.
1.8 34
Notes
• In Python, syntax errors are actually treated like runtime errors
because they are detected by the interpreter when the program is
executed.
• In general, syntax and runtime errors are easy to find and easy to
correct, because Python gives indications as to where the errors
came from and why they are wrong.
• Finding logic errors, on the other hand, can be very challenging.
1.8 35
Check Point
If you forget to put a closing quotation mark on a string, what kind of
error will be raised?
Answer: Syntax Error
If your program needs to read data from a file, but the file does not
exist, an error would occur when running this program. What kind of
error is this?
Answer: Runtime Error
Suppose you write a program for computing the perimeter of a
rectangle and you mistakenly write your program so that it computes
the area of a rectangle. What kind of error is this?
Answer: Logic Error
1.8 36