Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08

Preview:

Citation preview

Useful IDLEUseful IDLEAlt-ENTER

Commenting-out

Writing a ProgramWriting a Program

CMSC 120: Visualizing Information

2/14/08

The Inner Workings: A Review

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

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

◦ **◦ *, /, %◦ +, -

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

ObjectsDynamic data type

◦Knows stuff◦Does stuff

◦A set of related data◦Operations (methods) needed to

access and manipulate that data

VariablesNames assigned to values

StoreIdentifyManipulate

Transfer◦Parameters

Blocks of CodeBlocks of CodeFunctions

◦ A set of commands that work together to complete a task

◦ Reuseable◦ Flexible◦ Define◦ Invoke

Modules◦ import

ProgramsAny set of instructions to the computerSelf-executing set of instructions to the

computer

# 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

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

ProgramsAny set of instructions to the computerSelf-executing set of instructions to the

computer

def main(): do something : do something

main()

THE ART OF THE ART OF PROBLEM SOLVINGPROBLEM SOLVING

Algorithm Design

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.

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

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.

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

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.

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

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

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

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)

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

# 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