Useful Python - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Useful Python

Description:

PI = 3.14. d = input('Enter a diameter:') print area(d), circumference(d) main ... Two Constants: pi, e. Using the math API. def calcBinSize(data, numBins): data.sort ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 21
Provided by: emilygreen
Category:
Tags: pi | python | useful

less

Transcript and Presenter's Notes

Title: Useful Python


1
Useful Python
  • CMSC 120 Visualizing Information

2
Scope
  • def 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()

3
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()

4
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()

5
Our Histogram Problem
Histogram
Which parts of the program need to access the
window? the offset?
(offset, offset)
(0,0)
6
Our Histogram Problem
Histogram
Ticks Labels Title
(0,0)
7
Doing Math
  • Pylab
  • math library
  • an application programming interface or API
  • set of useful functions
  • from math import
  • pi
  • 3.1415926535897931
  • functions/constants defined in an API turn purple

8
math API
  • pow(x, y)
  • returns xy.
  • sqrt(x)
  • returns the square root of x

9
math API
  • ceil(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.

10
math API
  • min(x)
  • returns minimum value in list x
  • max(x)
  • returns maximum value in list x
  • sum(x)
  • returns sum of values in list x

11
math API
  • Trigonometric functions
  • sin
  • cos
  • asin
  • acos
  • tan
  • atan
  • degrees
  • Radians
  • Logarithm functions
  • Two Constants pi, e

12
Using the math API
  • def calcBinSize(data, numBins)
  • data.sort()
  • mn data0
  • mx datalen(data) - 1
  • return (mx - mn) / float(numBins)
  • d range(20, 40, 2)
  • calcBinSize(d, 4)
  • 4.5

13
Using the math API
  • from math import
  • def calcBinSize(data, numBins)
  • data.sort()
  • mn data0
  • mx datalen(data) - 1
  • return ceil((mx - mn) / float(numBins))
  • d range(20, 40, 2)
  • calcBinSize(d, 4)
  • 5.0

14
Using the math API
  • from math import
  • def calcBinSize(data, numBins)
  • rng max(data) min(data)
  • return ceil(rng / float(numBins))

15
Significant Digits
  • Digits in a number that are meaningful
  • Depend on number of significant digits in data
  • Significant Digits in Calculations
  • The 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.

16
Why Care?
  • 22/3.5
  • 6.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

17
Outputting Numbers
  • print 'The value of pi is ' , pi
  • 3.1415926535897931
  • Value of pi, 3 places after the decimal point
  • print 'The value of pi is 5.3f' pi
  • 3.142

18
Formatting
  • 'The value of pi is 5.3f' pi
  • contains a format specification for the
    value to be inserted at that point
  • Begins with - sign
  • Ends with f floating point value
  • Number between and f specifies the format

19
Formatting
  • 'The value of pi is 5.3f' (pi)
  • contains the value to be formatted

20
Numerical Specifications
  • f float
  • d integer
  • s string
  • print 'My choice was 10s' 'Rock'
  • My choice was Rock
  • Justification -,
  • print 'My choice was -10s' 'Rock'
  • My choice was Rock
Write a Comment
User Comments (0)
About PowerShow.com