1 / 16

Day 2 – Lesson 8 Fruitful Functions

Day 2 – Lesson 8 Fruitful Functions. Python Mini-Course University of Oklahoma Department of Psychology . Lesson objectives. Understand how to return values from functions in Python Create “fruitful” functions. The power function revisited. def power(base, exponent): val = base

leland
Download Presentation

Day 2 – Lesson 8 Fruitful Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Lesson objectives • Understand how to return values from functions in Python • Create “fruitful” functions Python Mini-Course: Day 2 - Lesson 8

  3. The power function revisited def power(base, exponent): val = base for x in range(1,exponent): val = val * base return val power(3,4) Python Mini-Course: Day 2 - Lesson 8

  4. 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 Python Mini-Course: Day 2 - Lesson 8

  5. The return statement • You can only return one value • That 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 Python Mini-Course: Day 2 - Lesson 8

  6. Exercise • Create a function that computes the distance between any two points when given their coordinates (in 2-D space) Python Mini-Course: Day 2 - Lesson 8

  7. Distance function def distance(x1, y1, x2, y2): """Return the distance between points (x1, y1) and (x2, y2) as a float""" return 0.0 Python Mini-Course: Day 2 - Lesson 8

  8. 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 Python Mini-Course: Day 2 - Lesson 8

  9. 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 Python Mini-Course: Day 2 - Lesson 8

  10. 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 Python Mini-Course: Day 2 - Lesson 8

  11. 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) Python Mini-Course: Day 2 - Lesson 8

  12. Creating Boolean functions • Functions that return a Boolean value (True or False) can be used in conditional statements (i.e. if…else…) Python Mini-Course: Day 2 - Lesson 8

  13. 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) Python Mini-Course: Day 2 - Lesson 8

  14. 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 Python Mini-Course: Day 2 - Lesson 8

  15. Next session • Iteration methods: • Recursion • Loops and conditional execution • The for loop • The while loop • Handling strings and text Python Mini-Course: Day 2 - Lesson 8

  16. Suggested exercises • Exercise 5.1 – Fermat's Last Theorem • Exercise 6.6 – Palindromes • Exercise 6.7 – is_power function • Turtle exercises from Chaps 4 & 5 Python Mini-Course: Day 2 - Lesson 8

More Related