1. Introduction

iona.edu

1. Introduction

Department of Computer ScienceDr. John S. Mallozzi1. IntroductionBasic concepts, Immediate mode, Overviewof programming in Python, Algorithmicproblem solving – basic strategiesCopyright © 2010 by John S. Mallozzi


• Hardware• Hardware details• Layout of a typical computer• Software• Binary code• Language levels• Low-level language• High-level language• Compilers and interpreters• Python2


• Physical components of a computer• Included:• Internal components capable of Storage of information Both data and programs Execution of logical commands (programs)• External components – devices – which connect theinternal components to the user, allowing input andoutput3


Copyright 2006, John Wiley and Sons, Inc.5


• Program – set of instructions that allows acomputer to perform a task• When a program is used to make the computerperform a task, the program is executed• Gives a computer its flexibility• Change the program; change the task that can beperformed• Programs can be used by anyone• Programs must be written by specially trainedand educated people – programmers6


• A computer is an electronic device – it doesn’t―understand‖ any language• Communication is by electronic switches (onoff)• Switch positions can be represented by(encoded using) the digits 1 (on) and 0 (off)• Two positions, so called binary code• Binary code is called machine language• This is the only language ―understood‖ by acomputer7


• Working in machine language is extremelytedious and error-prone for humans• Easier-to-use languages were developed• Low-level languages are like machine language, butallow 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 betranslated into machine language• Only machine-language can be executed8


• Assembly language• Labels can be used to help the programmerremember Instruction names (e.g., ―ADD‖ instead of 01000111) Names for places where data are stored (e.g., ―salary‖instead of 11011010)• Each instruction corresponds to an instructionin machine language, so translation isstraightforward• A program that does the translation is called anassembler• Result of assembly is an executable machinelanguageprogram9


• More like ordinary spoken language, butinflexible and unambiguous• Much easier for programmers to use than lowlevellanguage• Harder to translate into machine language• One statement generally corresponds to manyinstructions• A program that does this kind of translation iscalled either a compiler or an interpreter,depending on how the time of translation isrelated to the time of execution of the program10


• EnglishIf the interest rate is more than 100, print themessage ―Rate error‖• Machine language00010101 00101000 00010000 01100100 1010001111110000• Low-level LanguageiloadintRatebipush 100if_icmpgt 240• High-level language (Python)if intRate > 100:print "Rate error"11


• A compiler translates an entire program into a lowerlevellanguage program• Once the compiler is finished, you have a program in thelower-level language• This translated program may then be executed (or furthertranslated, if not yet in machine language)• An interpreter executes a program, translating as it goes(since only machine language can be executed)• You never have a low-level program• Each time the program is interpreted, it must be re-translated• Compilers are used for efficiency• Interpreters allow illusion of direct execution, eventhough translation is taking place – virtual machine12


• A popular high-level, interpreted language, whichis reasonably easy to learn• Has form close enough to English to be mucheasier to use than low-level languages• Is simpler in form than many other high-levellanguages, such as Java or C++• Is available as a free download• Comes with many online resources• Help• Modules – libraries of pre-written solutions• Once you learn to program in one language, youcan learn other languages much more easily13


• Python is a free download, and is easy to installand set up• Complete instructions are available via FTP• FTP, File Transfer Protocol, is a good way to downloadfiles from the Internet• For instructions on downloading and using Python, seeftp://ftp.cs.iona.edu/pub/jmallozzi/Tools/pythonsetup.htm• There is an FTP site for this course, from which allprograms, including solutions to homework, will beavailableftp://ftp.cs.iona.edu/pub/jmallozzi/CS201/14


• Idle• The ―super calculator‖• Prototyping15


A programmer’s editor and execution shellthat come with PythonFor many applications,You must be sure thatYou see this before youstart16


• Python’s immediate mode, as seen in Idle, is usedlike a calculator• You enter an expression at the prompt: >>>• After you press enter/return, the expression you enteredis evaluated, and the result is printed (in a distinct color)on the next line• Unlike a calculator, Python’s immediate modeallows much more, such as use of names for resultsand program steps, manipulation of text, etc.• Example of use:17


• Immediate mode allows you to do anything ofwhich the computer is capable• This allows you to ―try out‖ different ideasabout solving a problem• Once you have the idea, you can write aprogram, so that the method you have come upwith can be saved to be used and reused18


• Here is a mathematical formula:• In words: to add the integers from 1 up to somepositive integer, multiply that integer by the nextinteger, then take half the result• You cannot use a computer to prove this, butyou can use it to help you believe it• We will use some Python features in thisexample, but don’t worry about them (we’ll getto all eventually)—just focus on how we areusing the computer19


• Features of Python we will use:• range, followed by two integers in parentheses, gives youa list of the integers from the first integer to one less thanthe 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 /20


• Before doing anythingelse, add your name atthe top of the file in acomment, and save thefile• A comment is a linestarting with #• In school use your Udrive• Be sure the extensionof the file name is .py• If you don’t, text colorwill no longer show23


• Once the file has been saved, you can enterPython commands• Learning the commands available will takesome effort, but not nearly as much as for aspoken language• For now, enter the traditional first program:25


• First, save the file to preserve the changes• Next, use the Run menu, and choose RunModule• You will be returned to the Shell, and yourprogram will be executed26


• If you entered the program incorrectly, youwill get an error message instead• In that case, return to the editing window, fixthe program, and repeat the execution steps27


• There are two main kinds of errors• Incorrect Python Solution: find out how to do it correctly, then change theprogram and try again• Correct Python, but incorrect results Solution: carefully think again about what you wanted, and besure you used the right Python commands, in the right order• The first kind of error is frustrating at first, buteventually becomes easy to fix• The second kind is called a bug, and may cause a lotof trouble – best is to avoid these by being carefulabout your planning and logic from the start28


• Designing with algorithms• Idea of an algorithm• Example• Computational example• Another way – Turtle Graphics29


• The best language in which to plan is your ownnative, spoken language – result is an algorithm• Once the plan is done, you can convert it to aprogram – implementation of the algorithm• Another way to approach a problem is the use ofprototyping• Combining the two ideas, designing an algorithmand prototyping, can lead to a successful programto solve a problem• Key to all this is knowing what a computer can doand how to express the commands to make it do so30


• An algorithm is a step-by-step description of how tocomplete a task• Idea is to carefully specify all steps – do not expectlistener/reader/computer to fill in details Example: This is not an algorithmTo get to my house turn left twice, then slight right, thenright, and then into driveway• Humans are good at leaving out steps• Computers are terrible at filling in missing steps• A program is an algorithm that has beenimplemented – translated into a computerlanguage31


• Problem – Shopping: drive to bank, get money,then drive to a store to buy bread, then drivehome• 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 fora computer32


• Problem: Draw the letter E on the screen with 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 segment33


• Translation is easy using English!• Do this, but in comments• Fill in details later• Result is the ―skeleton‖ of a program, whichrecords the algorithm in comments34


• To draw a horizontal segment, print a number ofstars (we’ll use 7 for now) on a single line• To draw a vertical segment, print a single star oneach of a number (we’ll use 3) of lines35


• Computers are capable of graphics, allowing amore ―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 justenough to draw letter E• The algorithm is the same, but now we think ofdrawing the letter without lifting the pen from thepaper (there are other ways, of course)• We need only a few turtle commands to do this36


• We must get the TurtleGraphics library—thisis done by importing it• We ask for everything inthe library using * for thename of what to import:from turtle import *• We need thecommands forward,back, right, and left• We also use thehideturtle commandso that all we see is thedrawing37

More magazines by this user
Similar magazines