Lab #5-6 Follow-Up: More Python ; Images
description
Transcript of Lab #5-6 Follow-Up: More Python ; Images
![Page 1: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/1.jpg)
Lab #5-6 Follow-Up:
More Python; Images
![Page 2: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/2.jpg)
Part 1: Python Conditionals, Return Values, and
Lists
![Page 3: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/3.jpg)
if <CONDITION> : <statement>
<statement> ...
<statement>elif <CONDITION> : <statement>
... <statement>elif ... else:
<statement>...
<statement>
Conditionals: General Form
![Page 4: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/4.jpg)
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?")
Example
![Page 5: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/5.jpg)
x > y # x greater than yx < y # x less than yx >= y # x greater than or equal to yx <= y # x less than or equal to yx != y # x not equal to y
Comparison Operators
![Page 6: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/6.jpg)
x > y and y > zx > y or y > znot (x > y)
Logical / Boolean Operators
G. Boole(1815-1864)
![Page 7: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/7.jpg)
Logic Gates: The Building Blocks of Digital Circuits
http://volga.eng.yale.edu/uploads/Main/gate-symbols.png
![Page 8: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/8.jpg)
Logic Gates: The Building Blocks of Digital Circuits
http://volga.eng.yale.edu/uploads/Main/gate-symbols.png
http://cpuville.com/logic_gates.htm
resistortransistor
![Page 9: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/9.jpg)
# 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'
Putting It All Together
![Page 10: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/10.jpg)
# 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'
Less Is More!
![Page 11: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/11.jpg)
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
![Page 12: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/12.jpg)
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 ….
![Page 13: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/13.jpg)
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))
![Page 14: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/14.jpg)
Multiple return points
def absoluteValue(x):if x < 0:
return –xelif x > 0:
return xelif x == 0:
return 0
![Page 15: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/15.jpg)
Less is more!
def absoluteValue(x):if x < 0:
return –xelse:
return x
![Page 16: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/16.jpg)
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) ?
![Page 17: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/17.jpg)
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
![Page 18: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/18.jpg)
Putting it all together: Writing our own sqrt()function
Algorithm for computing the square root of a number N:
1. Start with a lower bound of zero and an upper bound equal to N2. While the square of their mean is too far from N, do the following:3. If squared mean is too big, use mean as the new upper bound4. Otherwise, use mean as new lower bound
What haven't we considered?
![Page 19: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/19.jpg)
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
![Page 20: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/20.jpg)
Python Lists• Computer Science consists mainly in
1. Putting data into lists2. Sorting the lists according to some criterion (e.g.
alphabetical order)3. 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
![Page 21: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/21.jpg)
Self-Test: Predict the result of each print()
family = ['Simon', 'Linda', 'Sam']print(family[0]) # random accessprint(family[1])print('Simon' in family) # searchprint('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)
![Page 22: Lab #5-6 Follow-Up: More Python ; Images](https://reader036.fdocuments.net/reader036/viewer/2022062501/56816919550346895de03706/html5/thumbnails/22.jpg)
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)