Welcome Party
Questions
Annoucements
pair programming survey
hw1 due 6:30pm Wednesday
quiz on Friday
Practice: order the following lines to correctly print the area of a circle with radius
r
1 print(“The area of the circle is”, area)
2 area = math.pi * r**2
3 import math
4 r = 7.5
Consider the documentation for
math.pow
math.
pow
(
x, y
)
Return
x
raised to the power
y
.
How would be change the above code to use
math.pow
instead of **
Problem: what icon to display in weather app?
Simplified problem: difference between current and “hot” temperature (i.e., should the app show the current temperature in
red
)
how would you demonstrate to me this is correct (or incorrect)
print everything, check for known results
compare to one-line version
print(“difference between hot and current temp in Celsius:”, (80 - 32) / 9 * 5 - get_cur_temp())
Elements of Style
Just like you want to avoid types, sloppy capitalization and uneven spacing in your writing, you should attempt to write code with
good style
Spaces around operators, equals signs, after commas
no space around ** is okay (e.g., x**2 + y**2)
Don’t cram too much on one line
harder to understand, more error prone, harder to debug
Use blank lines and comments to separate and label distinct parts
Numbers in Python
integers (
int
s) and decimal numbers (
float
s) are represented differently
use built-in function
int
to convert
will just throw away decimal part
int(5.9)
will return 5
Interpreter/REPL vs script
Functions!
returning to the
get_CCD_temp()
mystery
it’s called a
function
, not like in math class
separates definition and execution (you will see what this means in a moment)
why should Current Temperature Inc. have all the fun? Let’s make our own function!
F_to_C
function
start with diagram of input, output, operations
all
definitions
start out the same way (name, parameters, colon)
def <function name>(<parameter1>, <parameter2>, …):
parameters
number of parameters and their order
the labels they get inside the function
indent determines function body
using a function looks like
<function name>(<value for parameter1>, <value for parameter2>, …)
referred to as a
function call
Aaron’s function manifesto
only operate on parameters
always
return
result at the end
marks the end of a function and determines its value
what happens if these laws are disobeyed?
class discussion: why might we want to use functions?
Our weather app won’t convert from Fahrenheit to Celsius just once, but many many times
can we use
temp_F
or
temp_C
outside definition?
no, function bodies are their own little world
this is called
scope
and we’ll talk more about next week
1 F_to_C(temp_F):
2 temp_C = (temp_F - 32) * 5 / 9
3 return temp_C
4
5 hot_temp_C = F_to_C(80)
6 <rest of program>
In what order does the computer execute the above lines?
1 -> 5 -> 2 -> 3 -> 6
remember the instruction pointer? A function call and
return
are instructions that modify it
quick check: define a function that takes the radius and returns the area of a circle, use the function to print the area of a circle of radius 10
import math
def circle_area(radius):
area = math.pi * radius**2
return area
print(“Area of a circle with radius 10 is”, circle_area(10))