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

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

Feb 25, 2016

Download

Documents

Michelle McLean

Lab #5-6 Follow-Up: More Python ; Images. Part 1: Python Conditionals, Return Values, and Lists. Conditionals: General Form . if : ... elif : ... elif ... else: - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lab #5-6  Follow-Up: More Python ;  Images

Lab #5-6 Follow-Up:

More Python; Images

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

Part 1: Python Conditionals, Return Values, and

Lists

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

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

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

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

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

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

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

# 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

# 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

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

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

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

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

Less is more!

def absoluteValue(x):if x < 0:

return –xelse:

return x

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

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

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

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

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

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

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

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)