Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 6 Program Design 4/18/09 Python Mini-Course: Day 2 - Lesson 6 1
Lesson objectives 1. Describe the four major programming paradigms that can be used in Python 2. List the steps in the program design process 3. Use pseudocode to develop the logic for a simple algorithm 4/18/09 Python Mini-Course: Day 2 - Lesson 6 2
Sources Wikipedia /18/09 Python Mini-Course: Day 2 - Lesson 6 3
Programming paradigms The fundamental styles of computer programming Paradigms describe concepts and abstractions used to represent the elements of a program the steps that compose a computation 4/18/09 Python Mini-Course: Day 2 - Lesson 6 4
Programming paradigms Imperative / Procedural Fortran, COBOL, Pascal, BASIC Functional Mathematica, R Object-oriented (OOP) C++, Java, Python Event-driven 4/18/09 Python Mini-Course: Day 2 - Lesson 6 5
Procedural programming Programs are step-by-step instructions of how to perform a task Specify a series of algorithmic steps Emphasizes use of re-usable procedures (a.k.a functions) to change the program state 4/18/09 Python Mini-Course: Day 2 - Lesson 6 6
Procedural programming in Python # create empty list target = [] # iterate over each thing in source for item in source_list: trans1 = G(item) trans2 = F(trans1) # add transformed item to target target.append(trans2) 4/18/09 Python Mini-Course: Day 2 - Lesson 6 7
Functional programming Based on the lambda calculus Programs are based on the systematic evaluation of mathematical functions Programs describe what to do, not how to do it 4/18/09 Python Mini-Course: Day 2 - Lesson 6 8
Functional programming in Python # Define how to apply two generic # transformations compose2 = lambda A, B: \ lambda x: A(B(x)) # Apply two specific functions # F and G to each item in source target = \ map(compose2(F, G), source_list) 4/18/09 Python Mini-Course: Day 2 - Lesson 6 9
Object-oriented programming Programs are collections of “objects” that interact Objects have attributes and behaviors, much like things in the real world 4/18/09 Python Mini-Course: Day 2 - Lesson 6 10
OOP in Python Python is inherently OOP Everything in Python is an object We’ll discuss OOP in detail on Days 6 and 7 Example: see oop.py 4/18/09 Python Mini-Course: Day 2 - Lesson 6 11
Event-driven programming The flow of the program is determined by events user actions (mouse clicks, key presses, etc.) messages from other programs sensor outputs Used for GUIs 4/18/09 Python Mini-Course: Day 2 - Lesson 6 12
Event-driven programming Programs have two sections Event detection Event handling Example: StudyXXX.py 4/18/09 Python Mini-Course: Day 2 - Lesson 6 13
Program design process 1. Identify the program components you will need Functions, object classes, event handlers, etc. 2. Design the interfaces and stub them out Keep in mind the principles of encapsulation and generalization 4/18/09 Python Mini-Course: Day 2 - Lesson 6 14
Program design process 3. Write a test routine 4. Fill in the program components. Test and debug them 5. Write the main routine 6. Test and debug the program 4/18/09 Python Mini-Course: Day 2 - Lesson 6 15
Pseudocode Pseudocode is a compact and informal high-level description of an algorithm Uses the structural conventions of some programming language Is intended for human reading rather than machine reading Omits details that are not essential for human understanding of the algorithm 4/18/09 Python Mini-Course: Day 2 - Lesson 6 16
Pseudocode example bubbleSort for a list of sortable items: start loop set swapped = false for each item in the list (except the last one) if item > next_item then swap( item, next_item ) set swapped = true end if end for loop if swapped is true then repeat loop end loop 4/18/09 Python Mini-Course: Day 2 - Lesson 6 17
Using pseudocode Purpose Easy for humans to understand Is a compact and environment- independent description of the key principles of an algorithm Useful for: sketching out the structure of the program before coding documenting algorithms for publication 4/18/09 Python Mini-Course: Day 2 - Lesson 6 18