CS 201 Computer Science I Fall, 2010

Download Report

Transcript CS 201 Computer Science I Fall, 2010

Thanks to: Dr. John S. Mallozzi
Department of Computer Science
1.
2.
Introduction
Overview of programming in Python
•
•
•
Program – a set of instructions that allows a
computer to perform a task
When a program is used to make the computer
perform a task, the program is executed
Gives a computer its flexibility

•
•
Change the program; change the task that can be
performed
Programs can be used by anyone
Programs must be written by specially trained
and educated people – programmers
2
•
•
•
A computer is an electronic device – it doesn’t
“understand” any language
Communication is by electronic switches (on-off)
Switch positions can be represented by
(encoded using) the digits 1 (on) and 0 (off)
Two positions, so called binary code
 Recall: bit sequences can be written using
hexadecimal

•
Binary code is also called machine language

This is the only language “understood” by a
computer
3
•
•
Working in machine language is extremely
tedious and error-prone for humans
Easier-to-use languages were developed
Low-level languages are like machine language,
but allow the use of labels instead of binary
codes
 High-level languages are closer to spoken
languages, although without ambiguity and
much more limited

•
•
A program written in such a language must
be translated into machine language
Only machine-language can be executed
4
•
•
•
More like ordinary spoken language, but
inflexible and unambiguous
Much easier for programmers to use than
low-level languages (assembly) or
machine language
One statement generally corresponds to
many internal computer instructions
5
•
•
•
•
•
A popular high-level, interpreted language,
which is reasonably easy to learn
Has form close enough to English to be much
easier to use than low-level languages
Is simpler in form than many other high-level
languages, such as Java or C++
Is available as a free download
Comes with many online resources



Help
Tutorials and References
Modules – libraries of pre-written solutions
6
•
•
•
Python is a free download, and is easy to install
and set up if you want to. You can always use Lab
computers.
Instructions are available in textbooks (optional)
and online.
If you are going to install Python you can use the
instructions available at the following link:
http://www.iona.edu/faculty/mgnoutcheff/InstallingPython3.htm
7
IDLE - Immediate Mode
A programmer’s editor and execution shell that comes
with Python (be sure to use “Idle Basic” in the labs.)
For many applications,
You must be sure that
You see this before you
start
8
•
Python’s immediate mode, as seen in Idle,
can be used like a calculator


•
•
You enter an expression at the prompt: >>>
After you press enter/return, the expression
you entered is evaluated, and the result is
printed (in a distinct color) on the next line
Unlike a calculator, Python’s immediate
mode allows much more, such as use of
names for results and program steps,
manipulation of text, etc.
Example of use:
9
•
•
•
Immediate mode allows you to do
anything of which the computer is capable
This allows you to “try out” different ideas
about solving a problem
Once you have the idea, you can write a
program, so that the method you have come
up with can be saved to be used and
reused
10
•
Here is a mathematical formula:

•
•
In words: to add the integers from 1 up to some positive
integer, multiply that integer by the next integer, then
take half the result
You cannot use a computer to prove this, but you
can use it to help you believe it
We will use some Python features in this example,
but don’t worry about them (we’ll get to all
eventually)—just focus on how we are using the
computer
11
•
Features of Python we will use:

range, followed by two integers in parentheses, gives you a
list of the integers from the first integer to one less than the
second one
 Example: range(1,10) is [1,2,3,4,5,6,7,8,9]
To add all the numbers on a list, use sum
 To multiply, use * and to divide use /

12
•
•
•
•
•
•
Creating a file
Saving the file
Entering a program
Translating and executing the program
Testing and error correction
Three Rules Of Programming:




1) THINK before you program
2) A program is a human-readable essay on problem
solving that also executes on a computer.
3) The best way to improve programming and problemsolving skills is to Practice, Practice, Practice.
4) THINK before and WHILE you program
13
•
•
Once you
have a
series of
commands
that
perform a
desired
task, you
can save
them as a
program
From the
Idle menu,
choose New
Window
14
•
•
•
Before doing anything
else, add your full
heading at the top of
the file in a comment,
and save the file
Use your U drive!
Must add the .py
extension to the file
name. Not automatic!

If you don’t, text color
will no longer show
15
16
•
•
•
•
Once the file has been saved, you can enter
Python commands or statements
Learning the commands available will take some
effort, but not nearly as much as for a spoken
language
For now, enter the traditional first program:
print (“Hello World”)
Add another output statement with a custom
greeting of your own choosing.
17
•
•
•

Save the file to preserve the changes
Use the Run menu, and choose Run Module
(If you have not saved, will prompt to save)
You will be returned to the Shell, and your
program will be executed
18
•
•
If you entered the program incorrectly, you
will get an error message instead
In that case, return to the editing window, fix
the program, and repeat the execution steps
19
•
There are three main kinds of errors



•
•
Typos (Precision!) IDLE editor can help spot and fix these
Incorrect Python
 Solution: find out how to do it correctly, then change
the program and try again
Correct Python, but incorrect results
 Solution: carefully think again about what you wanted,
and be sure you used the right Python commands, in
the right order
The second kind of error is frustrating at first, but
eventually becomes easy to fix
The third kind is called a bug, and may cause a lot
of trouble – best is to avoid these by being careful
about your planning and logic from the start
20
•
•
•
•
•
Designing with algorithms
Idea of an algorithm
Example
Computational example
Another way – Turtle Graphics
21
•
•
•
•
•
The best language in which to plan is your own
native, spoken language – result is an algorithm
Once the plan is done, you can convert it to a
program – implementation of the algorithm
Another way to approach a problem is the use of
prototyping
Combining the two ideas, designing an algorithm
and prototyping, can lead to a successful program
to solve a problem
Key to all this is knowing what a computer can do
and how to express the commands to make it do so
22
•
An algorithm is a step-by-step description
of how to complete a task

Idea is to carefully specify all steps – do not
expect listener/reader/computer to fill in
details
 Example: This is not an algorithm
To get to my house turn left twice, then slight
right, then right, and then into driveway
Humans are good at leaving out steps
 Computers are terrible at filling in missing
steps

•
A program is an algorithm (or several
algorithms) that has been implemented –
translated into a computer language
23
•
•
Problem – Shopping: drive to bank, get
money, then drive to a store to buy bread,
then drive home
Algorithm:







•
Drive to bank
Go into bank
Get money out of bank
Drive to store
Go into store
Buy bread
Drive home
Too much detail for a human, still too little
for a computer
24
•

Problem: Draw the
letter ‘E’ on the
screen by using stars
(asterisks):
Solution: (keeping in mind that printing is done left-toright, top-to-bottom)
 Print top horizontal segment
 Print first short vertical segment
 Print middle horizontal segment
 Print second short vertical segment
 Print bottom horizontal segment
25
•
•
•
•
Translation is easy using English!
Do this, but in comments
Fill in details later
Result is the “skeleton” of a program, which records the
algorithm in comments
26
•
•
To draw a horizontal
segment, print a
number of stars ( Use
7 for now) on a
single line
To draw a vertical
segment, print a
single star on each of
a number (we’ll use
3) of lines
27
•
•
•
•
•
•
Computers are capable of graphics, allowing a
more “natural” way to make a letter E
Python comes with Turtle Graphics
Idea is to use angles and distances to move a
“turtle” which draws as it moves
Lab is the place for detail; for now we do just
enough to draw letter E
The algorithm is the same, but now we think of
drawing the letter without lifting the pen from the
paper (there are other ways, of course)
We need only a few turtle commands to do this
28
•
We must get the
Turtle Graphics
library—this is done
by importing it

We ask for everything
in the library using *
for the name of what to
import:
from turtle import *
•
•
We need the
commands forward,
back, right, and left
We also use the
hideturtle()
command so that all
we see is the drawing
29
30