thanks to: dr. john s. mallozzi department of computer science 1. introduction 2. overview of...
TRANSCRIPT
Thanks to: Dr. John S. MallozziDepartment of Computer Science
1. Introduction2. Overview of programming in
Python
Software
2
• 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
Binary Code
3
• 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
Language Levels
4
• 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
High-level Language
5
• 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
Python
6
• 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
Obtaining and Starting Python
7
• 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
IDLE - Immediate Mode
A programmer’s editor and execution shell that comes with Python (be sure to use “Idle Basic”
in the labs.)
8
For many applications,You must be sure thatYou see this before youstart
IDLE - The “Super Calculator”
9
• Python’s immediate mode, as seen in Idle, can be used like a calculatorYou 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:
IDLE - Prototyping
10
• 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
Example
11
• 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
2)1(...321 nnn
Example (continued)
12
• 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 /
Overview of Programming in Python
13
• 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
problem-solving skills is to Practice, Practice, Practice.
4) THINK before and WHILE you program
Creating a File
14
• 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
Saving the File
15
• 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
Saving the File (filename.py)
16
Entering a Program
17
• 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.
Executing the Program
18
• 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
Result
19
• 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
Testing and Error Correction
20
• 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
Algorithmic Problem Solving – Basic Strategies
21
• Designing with algorithms• Idea of an algorithm• Example• Computational example• Another way – Turtle Graphics
Designing with Algorithms
22
• 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
Idea of an Algorithm
23
• An algorithm is a step-by-step description of how to complete a taskIdea 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 stepsComputers are terrible at filling in
missing steps• A program is an algorithm (or
several algorithms) that has been implemented – translated into a computer language
Example
24
• 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
Computational Example—Algorithm
25
• Problem: Draw the letter ‘E’ on the screen by using stars (asterisks):
Solution: (keeping in mind that printing is done left-to-right, 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
Computational Example:Program Skeleton
26
• 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
Computational Example—Program
27
• 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
Another Way – Turtle Graphics
28
• 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
The Program
29
• 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
Result
30