Docsity

Python CS 199: Beyond Programming - Week 5/12/2009 - Prof. Lois M. Delcambre, Study notes of Computer Science

Partial preview of the text

Download Python CS 199: Beyond Programming - Week 5/12/2009 - Prof. Lois M. Delcambre and more Study notes Computer Science in PDF only on Docsity!

Beyond programming^ CS 199 Computer Science for Beginners^ Spring 2009 – Lois Delcambre^ Week

5/12/

Introduction

y

We’ve covered:^ ◦

Python data types

Python expressions

Python statements

y

We’ve written approximately 30programs – that allow students topractice the various data types,expressions, and statements

y

It’s time to step back; talk about this …

5/12/

You’ve learned a lot of Python

def, return, import, from – used when you

define functions and import functions inmodules

5/12/ and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield def finally in print

You’ve learned a lot of Python

Logical expressions that evaluate to true or

false

5/12/ and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield def finally in print

You’ve learned a lot of Python

5/12/ and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield def finally in print

print statement del and in – used with iterables (list, set,

tuple)

Python statements (documentation)

y

  1. Simple statements^ ◦ 6.1. Expression statements ◦ 6.2. Assignment statements ◦ 6.3. The assert statement ◦ 6.4. The pass statement ◦ 6.5. The del statement ◦ 6.6. The print statement ◦ 6.7. The return statement ◦ 6.8. The yield statement ◦ 6.9. The raise statement ◦ 6.10. The break statement ◦ 6.11. The continue statement ◦ 6.12. The import statement ◦ 6.13. The global statement ◦ 6.14. The exec statement

y

  1. Compound statements^ ◦ 7.1. The if statement ◦ 7.2. The while statement ◦ 7.3. The for statement ◦ 7.4. The try statement ◦ 7.5. The with statement ◦ 7.6. Function definitions ◦ 7.7. Class definitions

y

Top-level components ◦ 8.1. Complete Pythonprograms ◦ 8.2. File input ◦ 8.3. Interactive input ◦ 8.4. Expression input 5/12/

Many programming languages havethe statements that you’ve learned

y Java has:^ ◦ assignment statements ◦ for loops ◦ while loops ◦ do/while loops ◦ break ◦ if, else ◦ print ◦ return 5/12/

We haven’t talked about classes and objects(object-oriented programming or oop) y Object-oriented programming is oneparadigm for programming y Procedural programming is anotherparadigm for programming (we have beenusing Python in a procedural way; wewrite one Python statement afteranother) y Functional programming is anotherparadigm – where everything is a function(no loop; no assignment statements) 5/12/

Therefore … we need it to be:

y

It might be run… for a long time …unchanged. Have you heard about the Y2Kproblem? Correct – for all inputs

y

It might be part of something larger – thatevolves over time.

Testable(again & again)

y

It might be used by others, exactly as it iswritten – if you put it in a module.Readable & Understandable

y

It might be used and modified by others, e.g.,in an open source setting.Readable & Understandable

5/12/

How do we know a program iscorrect – for all inputs?

y In general, we can’t … y Alan Turing proved in 1936 that it is notpossible to write a program that can takeas input: a program and an input anddecide whether or not the program willfinish or will run forever. y This is called the “halting problem”. y Thus, we say it is “undecidable” 5/12/

What about testing?

y

Very important.

y

You should be systematic in your testing.Figure out your “boundary conditions”the smallest/largest input that yourprogram should work for. Then test:^ ◦

The smallest input

A range of typical/normal inputs; variations

The largest input

An input smaller than the smallest

An input larger than the largest

5/12/

Testing (cont.)

y

“Testing can never completely identify allthe defects within software.”

y

Som software is released for use before itis bug-free (in many cases).

y

Some software is life-critical so propertesting is really important.

y

[Note that for some programs – it ispossible to prove, mathematically, thatthey meet their specification.]

5/12/

So … how should we developsoftware? How do we begin?

y It is generally a bad idea to write one bigprogram that does everything. Why? ◦ People don’t want to read long programs ◦ Longer programs are (usually) more complexbecause some variables may be used in morethan one place; thus some parts of the programmay depend on others. y Given what we know so far in Python,…what language feature should we use?^ ◦ Functions! Each function can be written andtested. It doesn’t “see” variables except thosethat are input and those that it returns. 5/12/

So, how do we decide whichfunctions to write?

y Top-down design^ ◦ Break the big program into a series of smallones. (Assume, for the moment, that you – orsomeone else – will be able to write thefunctions that you need.) 5/12/