250 likes | 268 Views
Understand the basic concepts of writing a program, defining data types, working with expressions, and utilizing variables and functions to solve problems effectively. Learn the art of problem-solving, algorithm design, and top-down implementation for efficient programming.
E N D
Useful IDLE • Alt-ENTER • Commenting-out
Writing a Program CMSC 120: Visualizing Information 2/14/08
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
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 • ** • *, /, % • +, -
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
Objects • Dynamic data type • Knows stuff • Does stuff • A set of related data • Operations (methods) needed to access and manipulate that data
Variables • Names assigned to values • Store • Identify • Manipulate • Transfer • Parameters
Blocks of Code • Functions • A set of commands that work together to complete a task • Reuseable • Flexible • Define • Invoke • Modules • import
Programs • Any set of instructions to the computer • Self-executing set of instructions to the computer
# 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
# 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()
Programs • Any set of instructions to the computer • Self-executing set of instructions to the computer def main(): do something : do something main()
The Art of Problem Solving Algorithm Design
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.
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
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?
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
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.
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
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
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
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 * 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
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