Upload
gerald-fisher
View
222
Download
0
Embed Size (px)
DESCRIPTION
Global Variables PI = 3.14 def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A def circumference(diameter): return PI * diameter def main(): d = input('Enter a diameter:') print area(d), circumference(d) main()
Citation preview
Useful Python
CMSC 120: Visualizing Information
Scopedef area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A
def circumference(diameter): return PI * diameter
def main(): PI = 3.14 d = input('Enter a diameter:') print area(d), circumference(d) main()
Global VariablesPI = 3.14
def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A
def circumference(diameter): return PI * diameter
def main(): d = input('Enter a diameter:')
print area(d), circumference(d) main()
Parameters
def area(diameter, P): radius = diameter / 2.0 A = P * radius ** 2 return A
def circumference(diameter, P): return P * diameter
def main(): d = input('Enter a diameter:') print area(d, P), circumference(d, P)
main()
Our Histogram Problem
Histogram
(0,0)
(offset, offset)
Which parts of the program need to access:the window?the offset?
Our Histogram Problem
Histogram
(0,0)
TicksLabelsTitle
Doing MathPylabmath library
an application programming interface or APIset of useful functions
>>> from math import *>>> pi3.1415926535897931
functions/constants defined in an API turn purple
math APIpow(x, y)
returns x**y.
sqrt(x) returns the square root of x
math APIceil(x)
returns the ceiling of x as a float, the smallest integer value greater than or equal to x.
floor(x) returns the floor of x as a float, the largest integer value
less than or equal to x.
math APImin(x)
returns minimum value in list x
max(x)returns maximum value in list x
sum(x)returns sum of values in list x
math APITrigonometric functions
sincosasinacostanatandegreesRadians
Logarithm functionsTwo Constants: pi, e
Using the math API
def calcBinSize(data, numBins): data.sort() mn = data[0] mx = data[len(data) - 1]
return (mx - mn) / float(numBins)
>>> d = range(20, 40, 2)>>> calcBinSize(d, 4)4.5
Using the math APIfrom math import *
def calcBinSize(data, numBins): data.sort() mn = data[0] mx = data[len(data) - 1]
return ceil((mx - mn) / float(numBins))
>>> d = range(20, 40, 2)>>> calcBinSize(d, 4)5.0
Using the math API
from math import *
def calcBinSize(data, numBins): rng = max(data) – min(data) return ceil(rng / float(numBins))
Significant DigitsDigits in a number that are meaningful
Depend on number of significant digits in data
Significant Digits in CalculationsThe number of significant digits in an answer should
equal the least number of significant digits being multiplied or divided
The number of decimal places in the answer should be the same as the least number of decimal places in any of the numbers being added or subtracted.
Why Care?
>>> 22/3.56.2857142857142856
Computers have ability to calculate to a high degree of precision
But...they are producing more information than input
All those extra digits take up space
Outputting Numbers>>> print 'The value of pi is ' , pi3.1415926535897931
Value of pi, 3 places after the decimal point:
>>> print 'The value of pi is %5.3f' % pi3.142
Formatting
'The value of pi is %5.3f' % pi
<string> % <expression>
<string>: contains a format specification for the value to be inserted at that pointBegins with % - signEnds with f: floating point valueNumber between % and f specifies the format
Formatting
'The value of pi is %5.3f' % (pi)
<string> % <expression>
<expression>: contains the value to be formatted
Numerical Specificationsf: floatd: integers: string
>>> print 'My choice was %10s' % 'Rock'My choice was Rock
Justification: -, +
>>> print 'My choice was %-10s' % 'Rock'My choice was Rock