1 / 22

Lab #5-6 Follow-Up: More Python ; Images

Lab #5-6 Follow-Up: More Python ; Images. Part 1: Python Conditionals, Return Values, and Lists. Conditionals: General Form . if <CONDITION> : <statement> <statement> ... <statement> elif <CONDITION> : <statement> ... <statement> elif ... else: <statement>

bond
Download Presentation

Lab #5-6 Follow-Up: More Python ; Images

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. Lab #5-6 Follow-Up:More Python; Images

  2. Part 1: Python Conditionals, Return Values, and Lists

  3. Conditionals: General Form if <CONDITION> : <statement> <statement> ... <statement> elif <CONDITION> : <statement> ... <statement> elif ... else: <statement> ... <statement>

  4. Example if IQ > 140: print("OMG genius!") elif IQ > 130: print("Wicked smaht!") elif IQ > 120: print("Way above average.") elif IQ > 110: print("Still no slouch.") elif IQ > 100: print("College material.") elif IQ > 90: print("Hope springs eternal!") else: print("Dude, where's my car?")

  5. Comparison Operators x > y # x greater than y x < y # x less than y x >= y # x greater than or equal to y x <= y # x less than or equal to y x != y # x not equal to y

  6. Logical / Boolean Operators x > y and y > z x > y or y > z not (x > y) G. Boole (1815-1864)

  7. Logic Gates: The Building Blocks of Digital Circuits http://volga.eng.yale.edu/uploads/Main/gate-symbols.png

  8. Logic Gates: The Building Blocks of Digital Circuits http://cpuville.com/logic_gates.htm http://volga.eng.yale.edu/uploads/Main/gate-symbols.png resistor transistor

  9. Putting It All Together # The grading scale will be 93-100 A; 90-92 A-; 87-89 B+; # 83-86 B; 80-82 B-; 77-79 C+; 73-76 C; 70-72 C-; # 67-69 D+; 63-66 D; 60-62 D-; below 60 F. if average >= 93 and average <= 100: grade = 'A' elif average >= 90 and average <= 92: grade = 'A-' elif average >= 87 and average <= 89: grade = 'B+' elif average >= 83 and average <= 86: grade = 'B' elif average >= 80 and average <= 82: grade = 'B-' . . . elif average < 60: grade = 'F'

  10. Less Is More! # The grading scale will be 93-100 A; 90-92 A-; 87-89 B+; # 83-86 B; 80-82 B-; 77-79 C+; 73-76 C; 70-72 C-; # 67-69 D+; 63-66 D; 60-62 D-; below 60 F. if average >= 93: grade = 'A' elif average >= 90: grade = 'A-' elif average >= 87: grade = 'B+' elif average >= 83: grade = 'B' elif average >= 80: grade = 'B-' . . . else: grade = 'F'

  11. if average >= 93: grade = 'A' elif average >= 90: grade = 'A-' elif average >= 87: grade = 'B+' elif average >= 83: grade = 'B' elif average >= 80: grade = 'B-' . . . else: grade = 'F' Dimes Pennies Nickels Quarters

  12. Returning Values from Functions • For many robot applications, functions just need to cause side-effects: def wiggle(speed, waitTime): rotate(-speed) wait(waitTime) rotate(speed) wait(waitTime) stop() • Outside of robotics, functions are described by what value they return as well ….

  13. Returning Values from Functions def square(x): return x * x # without square(), we'd have to do this: dst = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) # instead of this: dst = sqrt(square(x1-x2) + square(y1-y2))

  14. Multiple return points def absoluteValue(x): if x < 0: return –x elif x > 0: return x elif x == 0: return 0

  15. Less is more! def absoluteValue(x): if x < 0: return –x else: return x

  16. Putting it all together: Writing our own sqrt()function • For perfect squares (0, 1, 4, 9, 16, 25, …) , we typically just memorize them • For other numbers, we need a function: • from math import sqrt • How does sqrt() work? E.g., what is sqrt(3) ?

  17. Putting it all together: Writing our own sqrt()function • How does sqrt() work? E.g., what is sqrt(3) ? • It must be between 0 and 3, since a square root can't be negative, and a number can't be bigger than its own square root (?) • So our first approximate could just be the average of 0 and 3: (0 + 3) / 2 = 1.5 • But 1.5 * 1.5 = 2.25, which is less than 3 • So the square root must lie between 1.5 and 3 • (1.5 + 3) / 2 = 2.25; 2.25 * 2.25 = 5.0625, too big! • So the square root must lie between 1.5 and 2.25 • Et cetera

  18. Putting it all together: Writing our own sqrt()function • Algorithm for computing the square root of a number N: • Start with a lower bound of zero and an upper bound equal to N • While the square of their mean is too far from N, do the following: • If squared mean is too big, use mean as the new upper bound • Otherwise, use mean as new lower bound • What haven't we considered?

  19. PRECISION = 0.000001 def mysqrt(n): if n < 1: lower = n upper = 1 else: lower = 1 upper = n while True: mean = (lower + upper) / 2.0 meansqr = mean * mean if abs(meansqr-n) < PRECISION: return mean if meansqr > n: upper = mean else: lower = mean

  20. Python Lists • Computer Science consists mainly in • Putting data into lists • Sorting the lists according to some criterion (e.g. alphabetical order) • Searching the sorted list to find an item of interest • For sorting and searching to be efficient, the lists must support random access : like RAM (vs. sequential access of disk, one byte at a time) • Python lists provide all these features for us • Python's range() function generates numerical lists of arbitrary size

  21. Self-Test: Predict the result of each print() family = ['Simon', 'Linda', 'Sam'] print(family[0]) # random access print(family[1]) print('Simon' in family) # search print('George' in family) cousins = ['Stephanie', 'Susan', 'Michael'] print(family + cousins) family.sort() print(family) print(len(family)) family.append('Sharon') print(family) family.reverse() print(family) for person in family: print(person)

  22. Self-Test: Predict the result of each print() for k in range(5): print(k) for k in range(3,7): print(k) for k in range(1,10,2): print(k)

More Related