Chapter 1
Introduction to Computers,
Programs, and Python
1
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).
To create a basic graphics program using Turtle (§1.9).
2
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.
3
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.
4
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.
5
Python’s History
created by Guido van Rossum in Netherlands in 1990
Open source
6
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.
7
Launch Python
You can use the following python online
compiler to run python program:
https://www.onlinegdb.com/
8
Launch Python IDLE
https://www.onlinegdb.com/
9
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
Run
using Office 2010, check PowerPoint2010.doc located in
the same folder with this ppt file.
10
animation
Trace a Program Execution
Execute a statement
# Display two messages
print("Welcome to Python")
print("Python is fun")
11
animation
Trace a Program Execution
Execute a statement
# Display two messages
print("Welcome to Python")
print("Python is fun")
12
Two More Simple Examples
WelcomeWithThreeMessages Run
ComputeExpression Run
13
Companion
Website Supplements on the Companion
Website
See Supplement I.B for installing and
configuring Python
See Supplement I.C for developing
Python programs from Eclipse
www.cs.armstrong.edu/liang/py
14
Anatomy of a Python Program
Statements
Comments
Indentation
15
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")
16
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")
17
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
18
Programming Style and
Documentation
AppropriateComments
Proper Indentation and Spacing
Lines
19
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.
20
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.
21
Programming Errors
Syntax Errors
Error in code construction
Runtime Errors
Causes the program to abort
Logic Errors
Produces incorrect result
22
Getting Started with GUI Programming
Why GUI? Turtle Tkniter
GUI is a great pedagogical tool to motivate studetns
and stimulate student interests in programming.
23
Getting Started with GUI Programming
Why GUI? Turtle Tkniter
A simple way to start graphics programming is to
use Python built-in Turtle package.
A Turtule Example
Run
24
Getting Started with GUI Programming
Why GUI? Turtle Tkniter
Later in the book, we will also introduce Tkinter for
developing comprehensive GUI applications.
A Tkinter Example
Run
25