Download presentation

Presentation is loading. Please wait.

Published byBlake Tabor Modified over 3 years ago

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

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

3
if :... elif :... elif... else:... Conditionals: General Form

4
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

5
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 Comparison Operators

6
x > y and y > z x > y or y > z not (x > y) Logical / Boolean Operators 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://volga.eng.yale.edu/uploads/Main/gate-symbols.png http://cpuville.com/logic_gates.htm resistor transistor

9
# 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

10
# 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!

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: 1.Start with a lower bound of zero and an upper bound equal to N 2.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 bound 4. 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 1.Putting data into lists 2.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

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)

Similar presentations

OK

Cmpt-225 Simulation. Application: Simulation Simulation A technique for modeling the behavior of both natural and human-made systems Goal Generate.

Cmpt-225 Simulation. Application: Simulation Simulation A technique for modeling the behavior of both natural and human-made systems Goal Generate.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on rc phase shift oscillator application Ppt on operating system free download Ppt on power purchase agreement Ppt on online shopping project in java Ppt on event driven programming concept Ppt on natural disasters free download Ppt on non renewable resources Ppt on eddy current resistance Ppt on 2nd world war countries One act play ppt on dvd