Download presentation
Presentation is loading. Please wait.
Published byJack Patterson Modified over 9 years ago
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() >>> from plotDrugData import * >>> plotDrugData(Marijuana, ' Marijuana ' ) >>> from plotDrugData import * >>> plotDrugData(Marijuana, ' Marijuana ' ) Define 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 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? NOT how to do the job.
18
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 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 1. 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 type The Visualization
21
Develop an Algorithm 1. 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
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) 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() 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
# 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 # 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 1. What big steps do I need to take to accomplish the task? Set Method Calculate Percentage Plot the Percentage
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.