* Welcome Party
* Questions
V Annoucements
* pair programming survey
* hw1 due 6:30pm Wednesday
* quiz on Friday
V 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
V 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 **
V Problem: what icon to display in weather app?
V Simplified problem: difference between current and “hot” temperature (i.e., should the app show the current temperature in red)
V how would you demonstrate to me this is correct (or incorrect)
* print everything, check for known results
V compare to one-line version
* print(“difference between hot and current temp in Celsius:”, (80 - 32) / 9 * 5 - get_cur_temp())
V Elements of Style
V Just like you want to avoid types, sloppy capitalization and uneven spacing in your writing, you should attempt to write code with good style
V Spaces around operators, equals signs, after commas
* no space around ** is okay (e.g., x**2 + y**2)
V 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
V Numbers in Python
V integers (ints) and decimal numbers (floats) are represented differently
V use built-in function int to convert
* will just throw away decimal part
* int(5.9) will return 5
* Interpreter/REPL vs script
V Functions!
V 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)
V why should Current Temperature Inc. have all the fun? Let’s make our own function!
V F_to_C function
* start with diagram of input, output, operations
V all definitions start out the same way (name, parameters, colon)
* def <function name>(<parameter1>, <parameter2>, …):
V parameters
* number of parameters and their order
* the labels they get inside the function
* indent determines function body
V using a function looks like
<function name>(<value for parameter1>, <value for parameter2>, …)
* referred to as a function call
V Aaron’s function manifesto
* only operate on parameters
V always return result at the end
* marks the end of a function and determines its value
* what happens if these laws are disobeyed?
V 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
V 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
V 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
V 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))