Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful...

16
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1

Transcript of Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful...

Page 1: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Python Mini-CourseUniversity of Oklahoma

Department of Psychology

Day 2 – Lesson 8Fruitful Functions

05/02/09Python Mini-Course: Day 2 - Lesson 81

Page 2: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Lesson objectives

1. Understand how to return values from functions in Python

2. Create “fruitful” functions

05/02/09Python Mini-Course: Day 2 - Lesson 82

Page 3: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

The power function revisited

def power(base, exponent): val = base for x in range(1,exponent): val = val * base return val

power(3,4)

05/02/09Python Mini-Course: Day 2 - Lesson 83

Page 4: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

The return statement

Syntax return [expression]Exists the enclosing function and returns the value of expression as a result of the call to the function

If expression is omitted, returns None

05/02/09Python Mini-Course: Day 2 - Lesson 84

Page 5: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

The return statement

You can only return one valueThat value can be any Python data type (including both built-in and custom data structures)

You can also use the return statement to exit a function early

05/02/09Python Mini-Course: Day 2 - Lesson 85

Page 6: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Exercise

Create a function that computes the distance between any two points when given their coordinates (in 2-D space)

05/02/09Python Mini-Course: Day 2 - Lesson 86

Page 7: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Distance function

def distance(x1, y1, x2, y2):

"""Return the distance between points

(x1, y1) and (x2, y2) as a float"""

return 0.0

05/02/09Python Mini-Course: Day 2 - Lesson 87

Page 8: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Distance function

def distance(x1, y1, x2, y2):

"""Return the distance between points

(x1, y1) and (x2, y2) as a float"""

dx = x2 - x1

dy = y2 - y1

print 'dx is ', dx

print 'dy is ', dy

return 0.0

05/02/09Python Mini-Course: Day 2 - Lesson 88

Page 9: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Distance function

def distance(x1, y1, x2, y2):

"""Return the distance between points

(x1, y1) and (x2, y2) as a float"""

dx, dy = x2 - x1, y2 - y1

result = math.sqrt(dx**2 + dy**2)

return result

05/02/09Python Mini-Course: Day 2 - Lesson 89

Page 10: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Distance function

def distance(x1, y1, x2, y2):

"""Return the distance between points

(x1, y1) and (x2, y2) as a float"""

result = \

math.sqrt((x2-x1)**2 + (y2-y1)**2)

return result

05/02/09Python Mini-Course: Day 2 - Lesson 810

Page 11: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Distance function

def distance(x1, y1, x2, y2):

"""Return the distance between points

(x1, y1) and (x2, y2) as a float"""

return math.sqrt((x2-x1)**2 + (y2-y1)**2)

05/02/09Python Mini-Course: Day 2 - Lesson 811

Page 12: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Creating Boolean functions

Functions that return a Boolean value (True or False) can be used in conditional statements (i.e. if…else…)

05/02/09Python Mini-Course: Day 2 - Lesson 812

Page 13: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

is_divisible function

def is_divisible(x, y): if x % y == 0: return True else: return False

is_divisible(16,4)is_divisible(16,3)

05/02/09Python Mini-Course: Day 2 - Lesson 813

Page 14: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

is_divisible function

def is_divisible(x, y): return x % y == 0

is_divisible(16,4)is_divisible(16,3)x, y = 16, 5 if is_divisible(x, y): print x,'is divisible by',y

05/02/09Python Mini-Course: Day 2 - Lesson 814

Page 15: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Next session

Iteration methods:RecursionLoops and conditional execution

The for loopThe while loop

Handling strings and text

05/02/09Python Mini-Course: Day 2 - Lesson 815

Page 16: Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Suggested exercises

Exercise 5.1 – Fermat's Last TheoremExercise 6.6 – PalindromesExercise 6.7 – is_power functionTurtle exercises from Chaps 4 & 5

05/02/09Python Mini-Course: Day 2 - Lesson 816