useful python cmsc 120: visualizing information. scope def area(diameter): radius = diameter / 2.0...
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()TRANSCRIPT
![Page 1: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/1.jpg)
Useful Python
CMSC 120: Visualizing Information
![Page 2: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/2.jpg)
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()
![Page 3: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/3.jpg)
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()
![Page 4: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/4.jpg)
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()
![Page 5: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/5.jpg)
Our Histogram Problem
Histogram
(0,0)
(offset, offset)
Which parts of the program need to access:the window?the offset?
![Page 6: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/6.jpg)
Our Histogram Problem
Histogram
(0,0)
TicksLabelsTitle
![Page 7: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/7.jpg)
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
![Page 8: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/8.jpg)
math APIpow(x, y)
returns x**y.
sqrt(x) returns the square root of x
![Page 9: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/9.jpg)
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.
![Page 10: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/10.jpg)
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
![Page 11: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/11.jpg)
math APITrigonometric functions
sincosasinacostanatandegreesRadians
Logarithm functionsTwo Constants: pi, e
![Page 12: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/12.jpg)
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
![Page 13: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/13.jpg)
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
![Page 14: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/14.jpg)
Using the math API
from math import *
def calcBinSize(data, numBins): rng = max(data) – min(data) return ceil(rng / float(numBins))
![Page 15: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/15.jpg)
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.
![Page 16: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/16.jpg)
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
![Page 17: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/17.jpg)
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
![Page 18: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/18.jpg)
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
![Page 19: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/19.jpg)
Formatting
'The value of pi is %5.3f' % (pi)
<string> % <expression>
<expression>: contains the value to be formatted
![Page 20: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…](https://reader035.vdocuments.site/reader035/viewer/2022062601/5a4d1beb7f8b9ab0599e4093/html5/thumbnails/20.jpg)
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