Intro and beginning Python - Villanova Department of Computing

Download Report

Transcript Intro and beginning Python - Villanova Department of Computing

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
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.
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.
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.
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.
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.
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?
Basics (continued)
A very simplified block diagram of a computer
(numbers vary, but these are available now)
External storage
(CDs, DVDs, USB
drives)
2–8
full
CPUs
Inside the box
8 - 12 MB
3 – 64 GB
Central
Processing
Unit
Memory
Cache
Battery, fan,
various
connections
Keyboard, mouse,
touchpad, printer,
scanner …
Internal Hard
Drive
Some now have
flash drives
GB to TB sizes
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.
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.
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.
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
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
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
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.
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.
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.
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
Abstraction
An abstraction
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
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.
UML Class Diagrams
• Define the classes – types of objects – in a
system, and the relationships among them.
Class name
Attributes
Operations
Class diagram example
generalization
Many orders for 1
customer (many to
1 relationship)
Corporate customer and
personal customer inherit
characteristics from customer
http://atlas.kennesaw.edu/~dbraun/csis4650/A%26D/UML_tutorial/class.htm
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.
GCD: Algorithm
Book slide
27
GCD Algorithm (Euclid)
Book slide
28
Euclid's GCD
Values for u, v, and r starting with u = 54 and v = 42. Answer is ?
u
v
54
42
12
6
0
42
12
6
Book slide, modified
r
12
6
0
29
Try another
u
v
85
15
10
5
0
15
10
5
r
10
5
0
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.
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!
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.
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.
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.
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.
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.
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
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
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.
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
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
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
Practice problems
• Work on Practice problems 2.1, 2.3, 2.4 from
the text page 82