sequences cmsc 120: visualizing information 2/26/08

29
Sequences CMSC 120: Visualizing Information 2/26/08

Upload: annice-weaver

Post on 18-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

Graphics Window Step by Step def drawStuff(P1, P2, P3, win): # create a line L = Line(P2, P3) # create a circle C = Circle(P3, 10) C.setFill('red') # create a rectangle R = Rectangle(P1, P3) R.setFill('blue') # draw the objects L.draw(win) R.draw(win) C.draw(win)

TRANSCRIPT

Page 1: Sequences CMSC 120: Visualizing Information 2/26/08

Sequences

CMSC 120: Visualizing Information2/26/08

Page 2: Sequences CMSC 120: Visualizing Information 2/26/08

Step by StepAlgorithm: a set of sequential instructions that are

followed to solve a problem

Computer programs are algorithms that provide a set of sequential instructions to the CPU

CPU processes instructions one at a time, in the order that they are received; i.e., sequentially

Page 3: Sequences CMSC 120: Visualizing Information 2/26/08

Graphics Window

Step by Stepdef drawStuff(P1, P2, P3, win):

# create a lineL = Line(P2, P3)

# create a circleC = Circle(P3, 10)C.setFill('red')

# create a rectangleR = Rectangle(P1, P3)R.setFill('blue')

# draw the objectsL.draw(win)R.draw(win)C.draw(win)

Page 4: Sequences CMSC 120: Visualizing Information 2/26/08

Graphics Window

Step by Stepdef drawStuff(P1, P2, P3, win):

# create a lineL = Line(P2, P3)

# create a circleC = Circle(P3, 10)C.setFill('red')

# create a rectangleR = Rectangle(P1, P3)R.setFill('blue')

# draw the objectsC.draw(win)R.draw(win)L.draw(win)

Page 5: Sequences CMSC 120: Visualizing Information 2/26/08

The AlternativeSequential Processing

PerceptionProgramming

Parallel ProcessingPerceptionProgrammingMultiple CPUs

Control Flow Statements

Page 6: Sequences CMSC 120: Visualizing Information 2/26/08

Control FlowAlter the direction of the flow of the code

No longer sequential

Loops: repetition

Conditionals: make decisions

Gotos: jump to another section

Page 7: Sequences CMSC 120: Visualizing Information 2/26/08

RepetitionPrint out the numbers from 0 to 9

for-statementloop statementRepeat something a fixed number of times

>>> for i in range(10): print i

Page 8: Sequences CMSC 120: Visualizing Information 2/26/08

for-Statements

for <variable> in <seqeunce>: <do something> <do something> ...

Sets up number of times the loop is repeated or iterated

Body: stuff to be repeated

Page 9: Sequences CMSC 120: Visualizing Information 2/26/08

for-Statements

<variable> is the index for the loop

Assign successive values of <sequence> to <variable><sequence> is a list of values

Statements in the body are executed for each value

for <variable> in <seqeunce>:

Page 10: Sequences CMSC 120: Visualizing Information 2/26/08

RepetitionPrint out the numbers from 0 to 9

<sequence> is range(10)generates a sequence of numbers up to but not

including the specified parameter value>>> range(5)[0, 1, 2, 3, 4]

>>> for i in range(10): print i

Page 11: Sequences CMSC 120: Visualizing Information 2/26/08

RepetitionPrint out the numbers from 0 to 9

The index variable, i will take on the values of 0 through 9 and for each value, it will execute the print statement

>>> for i in range(10): print i

0123456789

Page 12: Sequences CMSC 120: Visualizing Information 2/26/08

ListsCollections of information

Any sequence of “things”NumbersLettersStringsPointsfigures

Page 13: Sequences CMSC 120: Visualizing Information 2/26/08

Types of ListsEmpty List

>>> [][]

or

>>> L = []>>> print L[]

An empty list does not contain anything

Page 14: Sequences CMSC 120: Visualizing Information 2/26/08

Types of Lists

N = [2, 43, 6, 27]FamousNumbers = [3.1415, 2.718, 42]Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin']MyPoints = [Point(100, 42), Point(75, 64), Point(83, 310)]MyCar = ['Mazda Protege', 1999, 'red']

Any collection of things, even those with different data types

Page 15: Sequences CMSC 120: Visualizing Information 2/26/08

Lists Operations

>>> N = [2, 43, 6, 27]>>> len(N)4

len(<sequence>): takes a list and returns the length, or the number of objects in the list

Page 16: Sequences CMSC 120: Visualizing Information 2/26/08

Lists Operations

>>> N = [2, 43, 6, 27]>>> FamousNumbers = [3.1415, 2.718, 42]>>> N + FamousNumbers[2, 43, 6, 27, 3.1415, 2.718, 42]

Concatenate (‘+’): joins two lists together

Page 17: Sequences CMSC 120: Visualizing Information 2/26/08

List Operations

>>> Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin']

>>> Cities[0]'Philadelphia'

Indexing: allows access individual elements in a list by their indexFirst element has an index of 0Last element has an index of n-1 where n is the length

of the list

Page 18: Sequences CMSC 120: Visualizing Information 2/26/08

Lists Operations

>>> Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin']

>>> Cities[1:3]['Hicksville', 'Troy']

Slice: allows access to a sublist by a set of indicesList[a:b] returns the elements from a to b-1

Page 19: Sequences CMSC 120: Visualizing Information 2/26/08

Lists Operations

>>> Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin']

>>> 'Troy' in CitiesTrue>>> 'Cairo' in CitiesFalse

in: checks whether an element is in a list

Page 20: Sequences CMSC 120: Visualizing Information 2/26/08

Lists are Objects

>>> N = [2, 43, 6, 27]>>> N.sort()>>> print N[2, 6, 27, 43]

sort: arrange elements in ascending order

Page 21: Sequences CMSC 120: Visualizing Information 2/26/08

Lists are Objects

>>> N = [2, 43, 6, 27]>>> N.reverse()>>> print N[27, 6, 43, 2]

reverse: reverse order of elements

Page 22: Sequences CMSC 120: Visualizing Information 2/26/08

Lists are Objects

>>> N = [2, 43, 6, 27]>>> N.append(3)>>> print N[2, 43, 6, 27, 3]

append: add an element to the end of the list

Page 23: Sequences CMSC 120: Visualizing Information 2/26/08

All Lists are SequencesAll lists can be used to perform repetitionsfrom graphics import *

MyPoints = [Point(100, 42), Point(75, 64), Point(83, 310)]

win = GraphWin('My Win', 400, 400)for point in MyPoints: c = Circle(point, 5) c.setFill('red') c.draw(win)

Page 24: Sequences CMSC 120: Visualizing Information 2/26/08

StringsStrings are also sequences

>>> ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'>>> len(ABC)26>>> for letter in ABC:

print letter

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Page 25: Sequences CMSC 120: Visualizing Information 2/26/08

Lists in PylabAll the data we have plotted so far has been stored

as a set of lists:

Year = [1979, 1980, 1982,…]Marijuana = [14.1, 13.3, 13.3, 12.5, …]

Page 26: Sequences CMSC 120: Visualizing Information 2/26/08

Lists in PylabThus, the actual syntax of the plot command is:

where the x- and y-data are actually lists of either int or float values

The syntax of the mean command is:

And the function calculates the mean of a list of int or float values

plot(<x-sequence>)Plot(<x-sequence>, <y-sequence>)

mean(<sequence>)

Page 27: Sequences CMSC 120: Visualizing Information 2/26/08

Lists in Pylab

Page 28: Sequences CMSC 120: Visualizing Information 2/26/08

Lists in PylabTicks:

Labels = ['Marijuana', 'Hallucinogens', 'Cocaine', 'Heroin', 'Cigarettes', 'Alcohol']

xticks(arange(6), Labels)

xticks(arange(<num-ticks>), <label-sequence>)

Page 29: Sequences CMSC 120: Visualizing Information 2/26/08

Lists in Pylab