Top Banner
Useful Python CMSC 120: Visualizing Information
20

Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…

Jan 20, 2018

Download

Documents

Gerald Fisher

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()
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…

Useful Python

CMSC 120: Visualizing Information

Page 2: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…

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…

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…

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…

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…

Our Histogram Problem

Histogram

(0,0)

TicksLabelsTitle

Page 7: Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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…

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