Professional Documents
Culture Documents
Topics
Introduction
How a Program Works
Using Python
Program development process
Good programming habits
2
Introduction to Computers and Programming
1
Introduction
Computers can be programmed
Designed to do any job that a program tells them to
Program: set of instructions that a computer follows to
perform a task
Commonly referred to as Software
Programmer: person who can design, create, and test
computer programs
Also known as software developer
3
Introduction to Computers and Programming
4
Introduction to Computers and Programming
2
Interpreters
Programs written in high-level languages must be
translated into machine language to be executed
Interpreter: translates and executes instructions in high-
level language program
Used by Python language
Interprets one instruction at a time
No separate machine language program
Source code: statements written by programmer
10001011
Source
Interpreters 11001101
Program
00110011
Machine
Language 5
Introduction to Computers and Programming
Interpreters
Syntax error: a misspelled key word, a missing punctuation
character, or the incorrect use of an operator.
prevents code from being translated
Example: English
He is handsome vs. He are handsome
6
Introduction to Computers and Programming
3
How a Program Works
Program must be copied from secondary memory to RAM
each time CPU executes it
CPU executes program in cycle:
7
Introduction to Computers and Programming
Interpreters
8
Introduction to Computers and Programming
4
What is Python?
1) General Purpose
• 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.
9
Introduction to Computers and Programming
What is Python?
2) Interpreted
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.
10
Introduction to Computers and Programming
5
What is Python?
3) 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.
11
Introduction to Computers and Programming
Python’s History
created by Guido van Rossum in Netherlands in 1990
Open source programming language.
He later named this language as Python after choosing the word
form a TV serial named Monty Python's Flying Circus.
On December 3, 2008, Python 3.0 (also called "Py3K") was
released. It was designed to rectify fundamental flaw of the
language.
Python is influenced by following programming languages :ABC
language and Modula-3.
12
Introduction to Computers and Programming
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.
13
Introduction to Computers and Programming
Using Python
Python must be installed and configured prior to use
One of the items installed is the Python interpreter
Python interpreter can be used in two modes:
Interactive mode: the interpreter waits for you to type
Python statements on the keyboard. Once you type a
statement, the interpreter executes it and then waits for you
to type another statement.
Script mode: the interpreter reads the contents of a file that
contains Python statements. The interpreter executes each
statement in the Python program as it reads it.
14
Introduction to Computers and Programming
7
Interactive Mode
When you start Python in interactive mode, you will see a
prompt(>>>)
Indicates the interpreter is waiting for a Python statement to
be typed
Prompt reappears after previous statement is executed
Error message displayed if you incorrectly type a statement
Good way to learn new parts of Python
15
Introduction to Computers and Programming
16
Introduction to Computers and Programming
8
The IDLE Programming Environment
IDLE (Integrated Development Program):
single program that provides tools to write, execute and test
a program
Automatically installed when Python language is installed
Runs in interactive mode
Has built-in text editor with features designed to help write
Python programs
17
Introduction to Computers and Programming
18
Introduction to Computers and Programming
9
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.
19
Introduction to Computers and Programming
Appropriate Comments
Special characters
20
Introduction to Computers and Programming
10
Comments: e.g.s.
# This program displays Welcome to Python
''' This program displays Welcome to Python and
Python is fun
'''
# line comment : Python interpreter ignores all text after #
# on the same line.
21
Introduction to Computers and Programming
22
Introduction to Computers and Programming
11
Proper Indentation and Spacing
Spacing
Use blank line to separate segments of the code.
Add a single space to both sides of an operator.
E.g.: print(3+4*4) Bad style
print(3 + 4 * 4) Good style
23
Introduction to Computers and Programming
Programming Errors
1. Syntax Errors
Error in code construction
e.g., misspellings, punctuation omissions, or using an
opening parenthesis without a corresponding closing
parenthesis
Example:
24
Introduction to Computers and Programming
12
Programming Errors
2. Runtime Errors
Causes the program to terminate abnormally.
occur while a program is running if the Python
interpreter detects an operation that is impossible to
carry out.
e.g.: misplaced of file, or division by zero.
25
Introduction to Computers and Programming
Programming Errors
3. Logic Errors
Produces incorrect result, e.g. wrong formulas
E.g.: ShowLogicErrors.py
Output :
Fahrenheit 35 is Celsius degree
-12.555555555555554
Correct answer should be 1.66.
Correct formula should be
print(5 / 9 * (35 - 32)) 26
Introduction to Computers and Programming
13
Summary
This chapter covered:
Fetch-decode-execute cycle
Installing Python and the Python interpreter modes
27
Introduction to Computers and Programming
14