Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.
-
Upload
jack-patterson -
Category
Documents
-
view
214 -
download
0
Transcript of Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.
![Page 1: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/1.jpg)
Useful IDLEUseful IDLEAlt-ENTER
Commenting-out
![Page 2: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/2.jpg)
Writing a ProgramWriting a Program
CMSC 120: Visualizing Information
2/14/08
![Page 3: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/3.jpg)
The Inner Workings: A Review
![Page 4: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/4.jpg)
StatementsExecutable codeA program is a sequence of one or
more statements
Do not produce valuesExecuted for their side effects
Simple Statements◦ Assignment: x = 5◦ Return: return x◦ Import: from pylab import *◦ Print: print x
![Page 5: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/5.jpg)
ExpressionsWhen executed, evaluates to a valueUsually 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
◦ **◦ *, /, %◦ +, -
![Page 6: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/6.jpg)
ValuesData
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
![Page 7: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/7.jpg)
ObjectsDynamic data type
◦Knows stuff◦Does stuff
◦A set of related data◦Operations (methods) needed to
access and manipulate that data
![Page 8: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/8.jpg)
VariablesNames assigned to values
StoreIdentifyManipulate
Transfer◦Parameters
![Page 9: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/9.jpg)
Blocks of CodeBlocks of CodeFunctions
◦ A set of commands that work together to complete a task
◦ Reuseable◦ Flexible◦ Define◦ Invoke
Modules◦ import
![Page 10: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/10.jpg)
ProgramsAny set of instructions to the computerSelf-executing set of instructions to the
computer
![Page 11: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/11.jpg)
# File: plotDrugData.py# Purpose: A useful function # Author: Emily Allen
# import supporting filesfrom pylab import * from DrugData import *
# define the functiondef plotDrugData(data, name): plot(Year, data, '-rs') xlabel('Year') ylabel('Percentage') title(name + ' Drug Use 1979-2007') show()
>>> from plotDrugData import *>>> plotDrugData(Marijuana, 'Marijuana')>>> from plotDrugData import *>>> plotDrugData(Marijuana, 'Marijuana')
Define
Invoke
![Page 12: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/12.jpg)
# File: plotDrugData.py# Purpose: A useful function # Author: Emily Allen
# import supporting filesfrom pylab import * from DrugData import *
# define the functiondef plotDrugData(data, name): plot(Year, data, '-rs') xlabel('Year') ylabel('Percentage') title(name + ' Drug Use 1979-2007') show()
def main(): plotDrugData(Marijuana, 'Marijuana')
main()
![Page 13: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/13.jpg)
ProgramsAny set of instructions to the computerSelf-executing set of instructions to the
computer
def main(): do something : do something
main()
![Page 14: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/14.jpg)
THE ART OF THE ART OF PROBLEM SOLVINGPROBLEM SOLVING
Algorithm Design
![Page 15: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/15.jpg)
A ProblemA ProblemYou 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.
![Page 16: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/16.jpg)
Top-Down DesignTop-Down DesignDefine the problem
Express the solution in terms of smaller problems.
Now, break down the smaller problems into smaller problems.
Repeat until they are trivial
![Page 17: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/17.jpg)
Defining the ProblemDefining the Problem
1. What is the task?2. What information is required?3. Is any of that information
missing?
4. Any inputs? Any outputs?5. What are the characteristics of
the system? Its elements?
What is the job?What is the job?
NOT how to do the job.
NOT how to do the job.
![Page 18: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/18.jpg)
Develop an Algorithm
1. What big steps do I need to take to accomplish the task?
2. How can I break those big steps down into easily solvable small steps?
3. How can I generate what I am missing from what I have?
Algorithm: set of sequential instructions that are followed to solve a problem
Algorithm: set of sequential instructions that are followed to solve a problem
Pseudocode: expression of an algorithm in
everyday language
Pseudocode: expression of an algorithm in
everyday language
![Page 19: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/19.jpg)
A ProblemA ProblemYou 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.
![Page 20: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/20.jpg)
Defining the ProblemDefining the Problem1. What is the task?
To calculate and visualize the % of live births per a given method versus Mother’s Age
2. What information is required?Percentage of births by a given method
3. Is any of that information missing?Percentage of births by a given method
4. Any inputs? Any outputs?Method typeThe Visualization
![Page 21: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/21.jpg)
Develop an Algorithm1. What big steps do I need to take to
accomplish the task? Set Method Calculate Percentage Plot the Percentage
2. How can I break those big steps down into easily solvable small steps?
Plot the Percentage Generate plot Label plot Show plot
3. How can I generate what I am missing from what I have?
Percentage = method / total
![Page 22: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/22.jpg)
PseudocodeSet methodCalculate the percentage
◦Percentage = method / totalGenerate Plot
◦plot Mother’s Age versus Percentage◦xlabel is Mother’s Age◦ylabel is Percentage◦title is the method◦show the plot
![Page 23: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/23.jpg)
Bottom-Up ImplementationOne task usually = one functionPick 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 totaldef calcPercentage(method): return percent(method, Total)
from LiveBirthData import *from percent import *
# function for calculating percent of totaldef calcPercentage(method): return percent(method, Total)
![Page 24: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/24.jpg)
from LiveBirthData import *from percent import *from pylab import *
# function for calculating percent of totaldef calcPercentage(method): return percent(method, Total)
# function for plottingdef plotPercentage(percentage, label): bar(MotherAge, percentage) xlabel('Age of Mother') ylabel('Percent Total Live Births') title = (label) show()
from LiveBirthData import *from percent import *from pylab import *
# function for calculating percent of totaldef calcPercentage(method): return percent(method, Total)
# function for plottingdef 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
![Page 25: Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.](https://reader030.fdocuments.in/reader030/viewer/2022032722/56649f455503460f94c678b4/html5/thumbnails/25.jpg)
# main programdef 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
# main programdef 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
1. What big steps do I need to take to accomplish the task? Set Method Calculate Percentage Plot the Percentage