Introduction to Computers and Python
Introduction to Computers and Python
Introduction to Computers,
Programs, and Python
SE 201 (Programming D r. H a n a d i H a k a m i
1) [Link]@[Link]
©
Software
Sections
• 1.2. What is a Computer?
• 1.3. Programming Languages
• 1.4. Operating Systems
• 1.5. The History of Python
• 1.6. Getting Started with Python
• A Simple Python Program
• Simple Examples
• Anatomy of a Python Program
• 1.7. Programming Style and Documentation
• 1.8. Programming Errors
Programs Check Points
Programs
• Program 1: Welcome with Two Messages
• Program 2: Welcome With Three Messages
• Program 3: Compute an Expression
3
Objectives
• To understand computer basics, programs, and operating systems (
1.2-1.4).
• To write and run a simple Python program (1.5).
• To explain the basic syntax of a Python program (1.5).
• To describe the history of Python (1.6).
• To explain the importance of, and provide examples of, proper
programming style and documentation (1.7).
• To explain the differences between syntax errors, runtime errors,
and logic errors (1.8).
4
Textbook
Introduction to Programming Using Python, By: Y. Daniel Liang
5
1.2. What is a
Computer?
• CPU
• Memory
• Storage Devices
• Output Devices
• Input Devices
6
What is a Computer?
• A computer is an electronic device that stores and processes data.
• A computer includes both hardware and software.
• In general, hardware comprises the visible, physical elements of
the computer, and software provides the invisible instructions that
control the hardware and make it perform specific tasks.
• A computer consists of: CPU, memory, storage devices (such as
disks and CDs), input devices (such as the mouse and keyboard),
output devices (such as monitors and printers), and
communication devices (such as modems and network interface
cards).
7
What is a Computer?
• A computer’s components are interconnected by a subsystem
called a bus.
• You can think of a bus as a sort of system of roads running among
the computer’s components; data and power travel along the bus
from one part of the computer to another.
• In personal computers, the bus is built into the computer’s
motherboard, which is a circuit case that connects all of the parts
of a computer together.
Bus
8
A computer consists of
9
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).
• 1 megahertz equaling 1 million pulses per second.
Bus
10
CPU
11
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.
Bus
12
Memory
13
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.
• 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
14
Storage Devices
15
Output Devices
• An output device is any device used to send data from a computer
to another device or user.
• Most computer data output that is meant for humans is in the
form of audio or video such as monitors.
• The monitor displays information (text and graphics).
• The resolution and dot pitch determine the quality of the display.
Bus
16
Output Devices
17
Input Devices
• An input device is any hardware device that sends data to a
computer.
• Input and output devices let the user communicate with the
computer.
• The most common input devices are keyboards and mice.
Bus
18
Input Devices
19
1.3. Programming
Languages
• Programs
• Machine Language
• Assembly Language
• High-Level Language
• Popular High-Level Languages
• Interpreting/Compiling Source Code
• Interpreting Source Code
• Compiling Source Code
20
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.
21
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
22
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
23
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
24
Popular High-Level
Languages
25
Interpreting/Compiling
Source Code
• The instructions in a high-level programming language
are called statements.
• A program written in a high-level language is called a
source program or source code.
• Because a computer cannot understand a source
program, a source program must be translated into
machine code for execution.
• The translation can be done using another programming
tool called an interpreter or a compiler.
26
Interpreting 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.
• Note that a statement from the source code may be
translated into several machine instructions.
27
Compiling Source Code
• A compiler translates the entire source code into a
machine-code file, and the machine-code file is then
executed.
28
1.4. Operating Systems
29
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 30
1.5. The History of Python
What is Python?
Python’s History
Python 2 vs Python 3
31
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 32
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.
• In a compiled language, the entire source code is
compiled and then executed altogether.
1.5 33
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.
1.5 34
Python’s History
Python is created by Guido van Rossum in
Netherlands in 1990.
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 35
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.
• For example, the following command works on Python
2, but it doesn't work on Python 3: print "Hello World".
• To get the previous command working on Python 3, you
can write it as the following: print("Hello World").
• We will learn and use Python 3 in this book.
1.5 36
1.6. Getting Started with
Python
Install Python
PyCharm IDE
Install PyCharm
Modes of Python Interpreter
Interactive vs Script Mode
37
Install Python
Go to [Link]/downloads and then download
and install the last version of Python 3.11.x for your
operating system.
See Lab 1 for more details.
1.6 38
PyCharm IDE
An integrated development environment (IDE) is an
application that provides comprehensive facilities to
programmers for software development.
IDEs are large size programs, and many of them are
not free.
For Python programmers, PyCharm is one of the best
IDE for Python.
Also, it has a free version called “Community Edition”.
In general, using IDEs are the best way to develop
programs especially mid-large programs.
1.6 39
Install PyCharm
Go to [Link]
and then download and install “Community” version.
See Lab 1 for more details.
1.6 40
Modes of Python
Interpreter
Interactive Mode Script Mode
• Interactive mode provides us with a quick way of
running blocks or a single line of Python code.
• The code executes via the Python Shell (also known
as Python Interactive Shell), which comes with
Python installation.
1.6 41
Modes of Python Interpreter
Interactive Mode Script Mode
• The >>> indicates that the Python shell is ready to
execute and send your commands to the Python
interpreter.
• The result is immediately displayed on the Python
shell as soon as the Python interpreter interprets
the command.
1.6 42
Modes of Python Interpreter
Interactive Mode Script Mode
• This is the normal mode where a python code is
written in a text file with a ‘.py’ extension, and
Python interpreter executes the file.
• The result of the code will be displayed after the
Python interpreter runs the file.
1.6 43
Interactive vs Script Mode
The key differences between programming in interactive
mode and programming in script mode:
1. In script mode, a file must be created and saved
before executing the code to get results. In
interactive mode, the result is returned
immediately after pressing the <enter>
key from the keyboard.
2. In script mode, you are provided with a direct way of
editing your code. This is not possible in interactive
mode.
1.6 44
A Simple Python Program
Program 1: Welcome with Two Messages
Creating and Editing Using PyCharm
Tracing The Program Execution
Code Tracing
45
Welcome with Two Messages
Program 1
The Solution:
LISTING 1.1 [Link]
60
Welcome With Three Messages
Program 2
Write a program that displays Welcome to Python ,
Programming is fun , and Problem Driven . The output
should be as the following:
Welcome to Python
Python is fun
Problem Driven
The Solution:
LISTING 1.2 [Link]
1 # Compute expression
2 print((10.5 + 2 * 3) / (45 - 3.5))
The output:
0.39759036144578314
The output:
63
Check Point
#1
Identify and fix the errors in the following code:
Solution:
The errors are the incorrect indentation
in line 2 and the punctuation (.) in line 3.
1 # Display two messages
2 print("Welcome to Python")
3 print("Python is fun")
Simple Examples 64
Check Point
#2
Show the output of the following code:
1 print("3.5 * 4 / 2 - 2.5 is")
2 print(3.5 * 4 / 2 - 2.5)
Solution:
3.5 * 4 / 2 - 2.5 is
4.5
Simple Examples 65
Anatomy of a Python Program
Statement
Indentation
Comment
Special Symbols
66
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“.
LISTING 1.1 [Link]
It is a statement
1 # Display two messages (action)
2 print("Welcome to Python")
3 print("Python is fun") It is a statement
(action)
Programming Style
Documentation
Appropriate Comments and Comment St
yles
Proper Indentation and Spacing
75
Programming Style
Programming style deals with what programs look like.
When you create programs with a professional
programming style, they not only execute properly but
are easy for people to read and understand.
This is very important if other programmers will access
or modify your programs.
1.7 76
Documentation
Documentation is the body of explanatory remarks and
comments pertaining to a program.
These remarks and comments explain various parts of the
program and help others understand its structure and
function.
As you saw earlier in the chapter, remarks and comments are
embedded within the program itself; Python’s interpreter
simply ignores them when the program is executed.
Good programming style and proper documentation make a
program easy to read and prevent errors.
Programming style and documentation are as important as
coding. In the following slides, there are a few guidelines.
1.7 77
Appropriate Comments and
Comment Styles
1.7 78
Proper Indentation and Spacing
Indentation
Indent four spaces.
Spacing
A consistent spacing style makes programs
clear and easy to read, debug, and maintain.
Use blank line to separate segments of the
code.
print(20+50) print(20 + 50)
print(20- 10) print(20 - 10)
print(60*5+30) print(60 * 5 + 30)
print("A","B") print("A" ,"B")
1.7 79
1.8. Programming Errors
Types of Programming Errors
Syntax Errors
Runtime Errors
Logic Errors
Notes
Check Point #3
80
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 81
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 82
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.
1.8 83
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 84
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 85
Logic Errors
Example
The following is a program that converts a temperature
(35 degrees) from Fahrenheit to Celsius.
LISTING 1.4 [Link]
1.8 86
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 87
Check Point
#3
If you forget to put a closing quotation mark on a string,
what kind of error will be raised?
Answer:
1.8 88
Check Point
#3
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
89
The End