csc 8000 in python lillian n cassel department of computing sciences villanova university

44
CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Upload: pierce-marshall

Post on 24-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

CSC 8000 in Python

Lillian N CasselDepartment of Computing Sciences

Villanova University

Page 2: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Course purpose

• This course is intended to prepare incoming graduate students for the programming expectations of masters level courses in computer science and software engineering.

• Official Description:– Programming in Java or another object-oriented language.

Program design with an emphasis on the object paradigm. Classic algorithms and data structures. Significant programming assignments are required.

• This semester: Programming in Python

Page 3: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Why Python?

• Python is an object-oriented language, so satisfies the basic requirements of the course.

• Python is a very powerful language with much less overhead than Java. It is easier to get to writing significant programs in Python than it is in Java.

• Learning more than one language is very important for computing professionals. We don’t teach Python in any other class, so we will learn it in this one.

Page 4: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Resources and Expectations

• Textbook: – Object-oriented programming in Python. Goldwasser and

Letscher. Pearson/Prentice Hall publisher. 2008– Note that it is about both Python and the object paradigm. We

will study both in depth.• Computers:

– Does everyone have a laptop? • Expectations:

– We will write and demonstrate programs of increasing significance during the semester. Each program will be demonstrated and described. A small class gives us the luxury of doing this regularly.

Page 5: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Schedule notes

• Class meets on Wednesday evenings. Attendance is required except for extraordinary reasons.

• I have some travel that will take me away some weeks during the semester. On each occasion, there will be a class, generally in distance learning mode. There will be no missed classes due to my travel.

Page 6: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Grading• Most weeks will include a brief quiz on the previous material.

This should take about 10 – 15 minutes. Quiz grades will count for 25% of the course grade. I hope to have no other exams.

• Most of the grade (65%) will come from programming projects. Early in the semester, there will be a small program due each week. Later, the programs will be a bit larger and the time allowed a bit longer. – Submitted programs must run, and must produce correct results, must

use meaningful variable names and include appropriate comments. – Programs with errors will be returned. There will be one opportunity

to correct and resubmit with a penalty. The penalty will depend on the severity of the error.

• 10% of the grade will come from active participation in class.

Page 7: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Slides -- sources

• The textbook comes with a set of slides. I will use some of them, make some others, modify some of the ones from the book, adapt others that I find elsewhere. The slides that come with the book were developed by – Terry Scott– University of Northern Colorado– 2007 Prentice Hall

• Slides from other sources will always be acknowledged with a suitable reference.

Page 8: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Finding a starting point

• To make the best use of our time, I would like to know something about you and your programming experience. The purpose of the questions is to establish where we are starting so that I can tell what we need to cover in the class. Please answer fully and honestly:– Have you done any programming, in any language? If so,

what language(s)? About how long was your longest program? (10, 100, 300, 500, 1000, >1000 lines?) Are you familiar with/ comfortable with the object paradigm?

– If no, what is your computer use experience?

Page 9: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Basics (continued)A very simplified block diagram of a computer (numbers vary, but these are available now)

Central Processing Unit

Cache

Memory

Inside the box

External storage(CDs, DVDs, USB drives)

2 – 8 full CPUs

8 - 12 MB

3 – 64 GBInternal Hard Drive

GB to TB sizes

Battery, fan, various connections

Some now have flash drives

Keyboard, mouse, touchpad, printer, scanner …

Page 10: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Starting, or reviewing

• The CPU contains all the processing components of the computer, as well as some very special, very accessible memory for temporary storage

• The internal memory of the computer contains all instructions to be executed and all data to be used during program execution.

• Other links and connectors allow communication with external storage, and I/O devices.

Page 11: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Example specification – the July 2011 version of the Apple MacBook Pro

Note references to dual core and quad core processors. This has important implications for application development.

Page 12: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Program development

• The processor in a computer is hard wired to execute a small set of basic commands– Add, multiply, change sign, logical operations (and,

or, not). Not much more than that.• Any problem that a computer can solve can be

solved using these simple commands, but the process is very tedious and subject to human error.

Page 13: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Programming languages

• Machine code is too difficult for most purposes• Assembly language provides human friendly

representations of the machine code, but still closely related to the machine code

• Higher level languages are – Oriented toward problem domains or solution

approaches– Transferrable among different types of computers

with different underlying architectures and codes

Page 14: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Language examples• Fortran (Formula Translator), designed for ease in representing

mathematical computations• COBOL (COmmon Business Oriented Language) – good for simple

manipulation of data fields and production of reports• C – invented for use in systems programming, providing the control of

the computer that comes from assembly code, but more human readable.

• Pascal – developed specifically for teaching programming• APL – very concise, special characters, designed for manipulating

matrices• Algol, PL/1, Ada – many more• Java – Object oriented, virtual machine concept• Python – also object oriented, different characteristics, less overhead

than Java

Page 15: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Compilers and Interpreters

• All code from high level languages must be translated to machine code for execution on a specific type of processor.

• Two basic approaches– Interpreter

• Read an instruction, interpret it, convert it, execute it• Next run of the program must do all the interpreting again.

– Compiler• Read the entire program, convert it to machine code, usually

doing some optimization. Prepare a file of executable code. • The executable code can be rerun without re-compiling

Page 16: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Data and Types• All data is stored as zeros and ones.

– So are all instructions!• A collection of bits can represent anything

– An integer– A picture– A sound– An instruction

• To be meaningful, a collection of bits must be interpreted correctly and used as intended.– If data that represents an integer is interpreted as an alphabetic

character, the result may be nonsense.– Some operations only make sense when a particular type of data is

used. We multiply numbers, not names, for example.

Page 17: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Data and Types

• Some types of data are common and programming languages are designed to work with them naturally– Integers, real numbers, alphanumeric characters,

etc.• Other types of data are specific to a particular

context or problem domain and are created when needed.– Account number, shopping cart, blog post, etc.

Page 18: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Operations, Control, Functions

• Programming languages allow the programmer to specify the operations to be carried out.

• Control structures allow specification of repetition, ordering of operations, etc.– Conditionals: do this only under these conditions– Looping: repeat this sequence of instructions while

this condition holds– Sub code: execute this other set of instructions,

then return here for the next instruction.

Page 19: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Libraries

• Some operations are so common that there are libraries of code to call on when they are needed, so that they do not have to be reinvented by each programmer.

• These libraries have operations around a common theme or type of environment or problem. For example: system calls, or text processing, or graphics

Page 20: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Abstraction

Page 21: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

An abstraction

Page 22: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Modeling and Planning

• Programming begins with a problem to solve, and a plan for solving it.

• Starting with code usually means missing something.

• Various tools for planning a problem solution– Flow charts show the steps in the code. Much like

programming, but language independent– UML (Unified Modeling Language) • Higher level planning tool

Page 23: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

The Object Paradigm

• Think of a problem in terms of objects, properties of those objects, actions carried out by or on those objects.

• A problem solution becomes a collection of objects and interactions among the objects.

• Objects are further characterized by type– Objects of the same type form a class• For example, we could have a class car and a particular

car would be an object of that class.

Page 24: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

UML Class Diagrams

• Define the classes – types of objects – in a system, and the relationships among them.

Class name

Attributes

Operations

Page 25: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Class diagram example

http://atlas.kennesaw.edu/~dbraun/csis4650/A%26D/UML_tutorial/class.htm

Many orders for 1 customer (many to 1 relationship)

generalization

Corporate customer and personal customer inherit characteristics from customer

Page 26: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Algorithms

• An algorithm is a step by step process for solving a class of problems.

• A useful algorithm must be correct, but also efficient.

• The book example of finding the greatest common divisor illustrates that the obvious solution is not always the right approach to take.

Page 27: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

27

GCD: Algorithm

Book slide

Page 28: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

28

GCD Algorithm (Euclid)

Book slide

Page 29: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

29

Euclid's GCD

Values for u, v, and r starting with u = 54 and v = 42. Answer is ?

Book slide, modified

u v r54 42 1242 12 612 6 0 6 0

Page 30: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Try another

u v r85 15 1015 10 510 5 0 5 0

Page 31: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Why does Euclid’s GCD algorithm work?

• Let r = remainder of u/v• Then u = k*v + r for some k (all values integer)• r = u – k*v– Anything that divides evenly into both u and v must also

divide evenly into r– Similarly, any divisor of v and r must also divide into u– Therefore, the gcd of v and r is the gcd of u and v

• Algorithm terminates: r<v. Each successive value of v is smaller, but not negative and so must reach 0.

Page 32: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Algorithm to program

• An algorithm is encoded as a program in a specific programming language.

• Languages have syntax – The requirements of form – punctuation, spacing, spelling, etc.

• Languages have semantics– What happens as a result of an instruction – values replaced,

computations done, comparisons made, etc.• Programs encode logic

– If the language is used perfectly, but the logic is faulty, the results will be wrong.

– Computers do exactly what you tell them to do, not necessarily what you intended for them to do!

Page 33: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Again, The Object Paradigm

• In some programming models, there is an emphasis on what is done– Procedural languages, functional languages

• In the Object Paradigm, there is an emphasis on the objects that are defined and manipulated in a program. – An object is an entity that has properties and for

which certain operations or actions are relevant– A class is a category of objects, of which a specific

object is an instance.

Page 34: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Classes

• Data and operations on that data are encapsulated to form a class.

• The data within a class are called attributes• All the values of the attributes (data) for an

instance (object) comprise the state of the object.

• The operations within a class are called methods.

Page 35: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Examples

• There are some examples in the chapter. • Let’s do something different, to provide

another example.• Imagine a class of objects that are DVDs– What are the attributes?– What methods (operations) would be relevant?

Work with one or two others and devise a set of DVD objects. Show sample attributes and methods.

Page 36: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Beginning with Python

• Python interpreter– If installed, just type python at the prompt

• Enter python instruction and get results

– Also, create a file of type .py with python code• Type python <filename.py> • Whole program run• The advantage of a file of python code is that you do not have to

keep entering the same instructions over and over again. However, the code is still interpreted – reconverted to machine language each time it is run.

• Python compiler available, but we will not bother now.

Page 37: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Hello world!

• Traditional first program in a new language prints the message “Hello, world!”

• This establishes that you can invoke the interpreter and get a program to run.

• It is also a measure of the complexity of the language at some level. How hard can it be to make a program print “Hello, world!”?– You might be surprised!

• Python program:– print “Hello, world!”– That’s it! Now, on to bigger and better things.

Page 38: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Class: list• We will have many occasions to use a list.• list() creates an empty list• movies = list() makes an empty list with the name (identifier) movies• What can we do to a list?

– append(x) – add an item, x, to the end of the list– extend(L) – Extend the list by appending all the items of list L– Insert(i,x) – insert item x at position i of the list– remove(x) – remove the first item in the list with value = x (error if none exist)– pop(i) – return item at location i (and remove it from the list) If no index (i) is

provided, remove and return the last item of the list.– index(x) – return the index value for the first occurrence of x – count(x) – return the number of occurrences of x in the list– sort() – sort the list, in place– reverse() – reverse the order of the elements in the list

Page 39: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

List behaviors

• See Figure 2.2 page 42 for further list behaviors– Mutators change an existing list– Accessors return information about a list– Generators create a new list

Page 40: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Special list from range

• We use a lot of lists. • The instruction range creates a list of numbers

with specified attributes• range(start,stop,inc)– Creates a lists of numbers starting with start, up to the

last value less than stop (not including stop), incrementing by inc

– range(3,20,2) = [3, 5, 7, 9, 11, 13, 15, 17, 19]– If only one number given, the list will go from 0 to one

less than the number, incrementing by one– If two numbers are given, they are assumed to be start

and stop, and the increment is 1.

Page 41: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Getting Documentation

• Note that you can get documentation about python from several places– Within the python interpreter: help(<topic>)• Ex. help(list.sort)

– From the Python main website • www.python.org

– Many online tutorials and reference sites

Page 42: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

List attributes

• What attributes does a list have?– len – length of the list; number of entries– sizeof – size of the list in bytes– count – number of occurrences of a particular

value in the list• A list item has – index value (position in the list)• The position counting starts with 0

– value

Page 43: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Basic I/O

• Programs are really limited if you cannot get information into and out of them.– Basic input:• raw_input(<text string to display>)• Ex: raw_input(“Enter your name: “)• Real use:

– name=raw_input(“Enter your name: “)

– Basic output:• print <list of items to print>• Ex: print “The name you entered is “, name

Page 44: CSC 8000 in Python Lillian N Cassel Department of Computing Sciences Villanova University

Practice problems

• Work on Practice problems 2.1, 2.3, 2.4 from the text page 82