Python conditionals, return values, and lists
Download
Report
Transcript Python conditionals, return values, and lists
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>
...
<statement>
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?")
Comparison Operators
x
x
x
x
x
> y
< y
>= y
<= y
!= y
#
#
#
#
#
x
x
x
x
x
greater than
less than y
greater than
less than or
not equal to
y
or equal to y
equal to y
y
Logical / Boolean
Operators
x > y and y > z
x > y or y > z
not (x > y)
G. Boole
(1815-1864)
Logic Gates:
The Building Blocks of Digital Circuits
http://volga.eng.yale.edu/uploads/Main/gate-symbols.png
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
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'
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'
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
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 ….
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))
Multiple return points
def absoluteValue(x):
if x < 0:
return –x
elif x > 0:
return x
elif x == 0:
return 0
Less is more!
def absoluteValue(x):
if x < 0:
return –x
else:
return x
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) ?
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
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?
PRECISION = 0.000001
def mysqrt(n):
if n < 1:
lower
upper
else:
lower
upper
= n
= 1
= 1
= 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
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
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)
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)