1 / 25

Useful IDLE

Useful IDLE. Alt-ENTER Commenting-out. Writing a Program. CMSC 120: Visualizing Information 2/14/08. The Inner Workings: A Review. Statements. Executable code A program is a sequence of one or more statements Do not produce values Executed for their side effects Simple Statements

Download Presentation

Useful IDLE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Useful IDLE • Alt-ENTER • Commenting-out

  2. Writing a Program CMSC 120: Visualizing Information 2/14/08

  3. The Inner Workings: A Review

  4. Statements • Executable code • A program is a sequence of one or more statements • Do not produce values • Executed for their side effects • Simple Statements • Assignment: x = 5 • Return: return x • Import: from pylab import * • Print: print x

  5. Expressions • When executed, evaluates to a value • Usually have no side effects • Order of operations: >>> x = 5*2+7/8-(3+mean(y))**7 • Start at innermost pair of parentheses and work your way out Evaluate functions to obtain their value • ** • *, /, % • +, -

  6. Values • Data • Types: • A classification that tells the computer what kind of data is being used • Limits potential values • Limits potential operation • Determines precision and Accuracy • int, long int, float, string, boolean

  7. Objects • Dynamic data type • Knows stuff • Does stuff • A set of related data • Operations (methods) needed to access and manipulate that data

  8. Variables • Names assigned to values • Store • Identify • Manipulate • Transfer • Parameters

  9. Blocks of Code • Functions • A set of commands that work together to complete a task • Reuseable • Flexible • Define • Invoke • Modules • import

  10. Programs • Any set of instructions to the computer • Self-executing set of instructions to the computer

  11. # File: plotDrugData.py # Purpose: A useful function # Author: Emily Allen # import supporting files from pylab import * from DrugData import * # define the function def plotDrugData(data, name): plot(Year, data, '-rs') xlabel('Year') ylabel('Percentage') title(name + ' Drug Use 1979-2007') show() Define >>> from plotDrugData import * >>> plotDrugData(Marijuana, 'Marijuana') Invoke

  12. # File: plotDrugData.py # Purpose: A useful function # Author: Emily Allen # import supporting files from pylab import * from DrugData import * # define the function def plotDrugData(data, name): plot(Year, data, '-rs') xlabel('Year') ylabel('Percentage') title(name + ' Drug Use 1979-2007') show() def main(): plotDrugData(Marijuana, 'Marijuana') main()

  13. Programs • Any set of instructions to the computer • Self-executing set of instructions to the computer def main(): do something : do something main()

  14. The Art of Problem Solving Algorithm Design

  15. A Problem • You have been provided with a record of numbers of life births by method of delivery and age of the mother for the state of California in 2004. Write a program that calculates the percentage of total live births by a method and plots the result versus Mother’s Age in a bar chart. The plot should be labeled and titled appropriately.

  16. Top-Down Design • Define the problem • Express the solution in terms of smaller problems. • Now, break down the smaller problems into smaller problems. • Repeat until they are trivial

  17. Defining the Problem What is the job? NOT how to do the job. What is the task? What information is required? Is any of that information missing? Any inputs? Any outputs? What are the characteristics of the system? Its elements?

  18. Develop an Algorithm Algorithm: set of sequential instructions that are followed to solve a problem • What big steps do I need to take to accomplish the task? • How can I break those big steps down into easily solvable small steps? • How can I generate what I am missing from what I have? Pseudocode: expression of an algorithm in everyday language

  19. A Problem You have been provided with a record of numbers of life births by method of delivery and age of the mother for the state of California in 2004. Write a program that calculates the percentage of total live births by a method and plots the result versus Mother’s Age in a bar chart. The plot should be labeled and titled appropriately.

  20. Defining the Problem • What is the task? • To calculate and visualize the % of live births per a given method versus Mother’s Age • What information is required? • Percentage of births by a given method • Is any of that information missing? • Percentage of births by a given method • Any inputs? Any outputs? • Method type • The Visualization

  21. Develop an Algorithm • What big steps do I need to take to accomplish the task? • Set Method • Calculate Percentage • Plot the Percentage • How can I break those big steps down into easily solvable small steps? • Plot the Percentage • Generate plot • Label plot • Show plot • How can I generate what I am missing from what I have? • Percentage = method / total

  22. Pseudocode • Set method • Calculate the percentage • Percentage = method / total • Generate Plot • plot Mother’s Age versus Percentage • xlabel is Mother’s Age • ylabel is Percentage • title is the method • show the plot

  23. Bottom-Up Implementation • One task usually = one function • Pick a task and write it, evaluate it, debug it, until it works • e.g., Calculate the percentage • When satisfied, begin the next task from LiveBirthData import * from percent import * # function for calculating percent of total def calcPercentage(method): return percent(method, Total)

  24. from LiveBirthData import * from percent import * from pylab import * # function for calculating percent of total def calcPercentage(method): return percent(method, Total) # function for plotting def plotPercentage(percentage, label): bar(MotherAge, percentage) xlabel('Age of Mother') ylabel('Percent Total Live Births') title = (label) show() • Once all tasks are completed, assemble the whole algorithm into a program

  25. What big steps do I need to take to accomplish the task? • Set Method • Calculate Percentage • Plot the Percentage # main program def main(): method = Cesarean # choose the method label = 'Cesarean'# set label # calculate the percentage percentage = calcPercentage(method) # plot the percentage plotPercentage(percentage, label) main() # run the program

More Related